I’m trying to find a RegEx that will allow the digits 0-9 ONLY and not special characters. Is this an impossibility since pressing Shift + 2 is still a digit?
I’ve used the following:
return /[0-9]|\./.test(String.fromCharCode(event.keyCode));
Which works fine except for the special characters that are still allowed.
Any input would be most helpful.
I think you are in the right track. Just add
!event.shiftKey:There are also
altKeyandctrlKeyif you need them.For
keyCodes coming form the numeric keypad, you need to compare against the numerickeyCodeinstead of using a Regex:This technique can also be used instead of the Regex you are using:
See http://unixpapa.com/js/key.html