1
0
Fork 0
mirror of https://github.com/iNavFlight/inav-configurator.git synced 2025-07-15 20:35:19 +03:00

stronger validation

This commit is contained in:
cTn 2014-01-16 01:58:07 +01:00
parent ac9725928d
commit cbdb7a53d8

37
main.js
View file

@ -79,19 +79,42 @@ $(document).ready(function() {
tab_initialize_default(); tab_initialize_default();
// listen to all input change events and adjust the value within limits if necessary // listen to all input change events and adjust the value within limits if necessary
$("#content").on("focus", 'input[type="number"]', function() {
var element = $(this);
var val = element.val();
if (!isNaN(val)) {
element.data('previousValue', parseFloat(val));
}
});
$("#content").on("change", 'input[type="number"]', function() { $("#content").on("change", 'input[type="number"]', function() {
var min = parseFloat($(this).prop('min')); var element = $(this);
var max = parseFloat($(this).prop('max')); var min = parseFloat(element.prop('min'));
var val = parseFloat($(this).val()); var max = parseFloat(element.prop('max'));
var step = parseFloat(element.prop('step'));
var val = parseFloat(element.val());
// only adjust minimal end if bound is set // only adjust minimal end if bound is set
if ($(this).prop('min')) { if (element.prop('min')) {
if (val < min) $(this).val(min); if (val < min) element.val(min);
} }
// only adjust maximal end if bound is set // only adjust maximal end if bound is set
if ($(this).prop('max')) { if (element.prop('max')) {
if (val > max) $(this).val(max); if (val > max) element.val(max);
}
// if entered value is illegal use previous value instead
if (isNaN(val)) {
element.val(element.data('previousValue'));
}
// if step is not set or step is int and value is float use previous value instead
if (isNaN(step) || step % 1 === 0) {
if (val % 1 !== 0) {
element.val(element.data('previousValue'));
}
} }
}); });
}); });