View Full Version : numeric entries from non-us keyboards and reg exp match

06-16-2003, 08:21 PM
I want to write a javascript function to disallow an entry field from accepting non-numeric entries (except of course, backspace, delete, and some other keys). I've seen some examples of using the event.keycode (or event.which) object to do this. My concern keyboards in different locales. I'm sure there are lots of you out there with different locale keyboards. Do the numeric key codes (key pad and regular numeric) have the same value on all keyboards?? What about keys like + -, up/down arrow, page up/page down, home, end, delete, backspace, insert???

I don't have access to keyboards from other locales so I must ask here. Is there some document which lists what the keycodes are in all locales (if they are not the same???).

Also, should I be using the keyup, keydown, or keypress event to filter out keys?

One last thing. Before I struggle to write one, does anyone have a generic javascript function which can be passed a regular expression and can determine on each keypress whether the current value matches, and reject non-matches??

06-16-2003, 09:10 PM
-post deleted because i'm a bad reader and gave the wrong answer-

06-16-2003, 09:17 PM
so is it pretty much a given that no matter what countries/locales keyboard is used, these values should be the same?

06-16-2003, 09:20 PM
thanx for the reply. One more thing.
If I'm going to try to do a regular expression matcher, am I correct that at the time I'm looking at event.keyCode, the text field .value property does not yet have the entered key? Or does that depend on whether the event is on a keydown, keyup, or keypress ???

06-16-2003, 09:29 PM
if you use regualr expressions, you let them enter it and then validate it.


06-16-2003, 09:38 PM
Here's a great keyCode tool:

It looks as though the pressed key is entered into the input and appended to its value immediately before the keyDown and keyPress events are triggered. be careful, though, because as you'll see on the above page, the keyCode for a keyPress event is different in many cases from the same key on a keyDown or keyUp event. However, I haven't seen any differences displayed between keyDown and keyUp events.

Good luck with the locale-specific script!

edited to add: Mozilla apparently doesn't register a keyCode on the keyPress event when a printable character is used. Rather, it does, but it's always the number 0 - you'll have to look at the charCode property of the event to see which key was pressed on keyPress in Mozilla.

Opera 6 doesn't do a damned thing on that page.

06-17-2003, 01:46 AM
Hi Wayne,

<input onkeyup="if(/\D/g.test(value))value=value.replace(/\D/g,'')">

( ) ( )

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum