0

I need to capture that pressed key is 0-9 including numbers from numpad. I wrote a function that checks it but it doesn't work on numpad digits

function getKeyVal(e) {
   var order = String.fromCharCode(e.keyCode);
   return /^\s*\d+\s*$/.test(order)
}

String.fromCharcode return "a" from numpad key 1.

Can anybody help?

exexzian
  • 7,644
  • 6
  • 42
  • 52
  • 1
    See http://www.quirksmode.org/js/keys.html and many similars. Getting character pressed is much more tricky, so your assuption that String.fromCharCode(e.keyCode) is always correct is unfortunatelly wrong. – Danubian Sailor May 15 '13 at 08:50

1 Answers1

0

You can use the onkeypress handler, or subtract 48 from the keycode. See this question:

Get Correct keyCode for keypad(numpad) keys

Community
  • 1
  • 1
Zissou
  • 227
  • 1
  • 8