jquery - prevent input character in input type number with knockout -


this question has answer here:

i must prevent user insert non-numeric value in input type text knockout:

here html:

<input data-bind="value: mymodel.mynumber, valueupdate: 'afterkeydown', event: { change: validatemynumber }" type="number" /> 

here js:

self.validatemynumber = function (data, event) {      var input = data.mymodel.mynumber();       if (input == "")       {        } }; 

i can't find logic write inside validatemynumber function... when press button, it not number lost old value in model , empty string... in textbox nothing changes instead... want if insert non-numeric value must block insert in textbox... how can do?

i use global rule input[type=number] elements. don't need specific knockout.

$(document).on('keypress change focus', 'input[type="number"]', function (e) {     var iskeypress = e.type === 'keypress';     var ischange = e.type === 'change';     var isfocus = e.type === 'focus' || e.type === 'focusin';     var chartyped = string.fromcharcode(e.which);     var $el = $(this);      // store original value revert change invalidates min     if (isfocus) {         this.originalval = $el.val();         return;     }      // block invalid keystrokes     if (iskeypress) {         // don't allow non-numeric chars         if (!/^[\.0-9]$/.test(chartyped)) {             e.preventdefault();             return;         }          // don't allow double periods (123.456.789)         if (chartyped === '.' && $el.val().indexof('.') > -1) {             e.preventdefault();             return;         }     }      // don't allow >max or <min values     if (ischange) {         var min = $el.attr('min');         var max = $el.attr('max');         var amt = parseint($el.val() + chartyped);          if (max && amt > max || min && amt < min) {             // revert change             $el.val(this.originalval);             $el.focus();         }     } }); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -