0

I'm trying to restrict a text box to numeric entry only in a project using knockout.js and MVC.

I'm doing a data-bind on the keypress event to call a function from the VM that looks like this:

this.NumbersOnly = function (data,evt) {        
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode != 46 && (charCode < 48 || charCode > 57)))
        return false;
    return true;
}

This will allow only numbers and decimals in to the input text box. This works great in every browser I've tested except that I'm having issues in Kindle's Silk browser, as I can still type alpha characters in. If anyone has any ideas to get Silk to agree, please let me know!

xdumaine
  • 10,096
  • 6
  • 62
  • 103
M.Bush
  • 1,102
  • 1
  • 12
  • 22
  • What is the value of `charCode` when the `if` statement is evaluated? – xdumaine Aug 12 '14 at 16:54
  • typically the keycode for whatever I've hit.. I suppose I need to figure out a way to debug in Silk – M.Bush Aug 12 '14 at 17:03
  • Right, that's what I mean. - what's the actual value, if not what you expect. – xdumaine Aug 12 '14 at 17:12
  • I'm not sure how you'd debug in silk either, but worst case, you could just alert the value. – xdumaine Aug 12 '14 at 17:12
  • Honestly I don't think Silk is actually getting in to the function, I'm about to try it in our staging enviornment using "keydown" instead of keypress, I'm reading in some places that "keypress" is deprecated – M.Bush Aug 12 '14 at 17:20
  • @M.Bush have you figured this out? – Viccari Apr 10 '17 at 04:42
  • Never did, we ended up not supporting Silk – M.Bush Jun 10 '17 at 21:17
  • Possible duplicate of [javascript (jquery) numeric input: keyCode for '3' and '#' are the same](https://stackoverflow.com/questions/8904763/javascript-jquery-numeric-input-keycode-for-3-and-are-the-same) – Paul Sweatte Jul 08 '17 at 18:59

0 Answers0