javascript - Jquery Regex for KeyUp event for an input box with a range numeric value -
i'm having spent several hours trying find way haven't been successful. want add keyup/keypress event accept value between 2 - 1827. i'm using aspx inputbox , here's have.
$('#field_txt').keyup(function () { var regex = /[^2-9]|[^1-9][^0-9]|[^1-9][^0-9][^0-9]|[^1][^0-8][^0-2][^0-7]/; var myregexp = /^([1-9]|1[0-9]|1[0-9][0-9]|[1-9][0-9][0-9]|1[0-8][0-2][0-7])$/g; if (!this.value.match(myregexp)) { this.value = this.value.replace(regex, ''); } });
if use regex expression, when user input 1 -19, doesn't work since expression matches false , value replace ''. however, if use 2nd regexp, use able enter 1.
i have looked @ other examples posted. html text input allow numeric input
thanks in advance.
regex seems hardest way possible accomplish this. following ensures entry both numeric , within range. field after you...
$('#field_txt').keyup(function() { var val = $(this).val(); var is_numeric = /^\d+$/gi.test(val); if (is_numeric) { var val = parseint(val); var within_range = (val >= 2 && val <= 1827); } if (!is_numeric || !within_range) { $(this).val(/* insert whatever value want here */); } });
Comments
Post a Comment