getkey() function does just that:
In its simplest use,
getkey() simply returns the numeric code for the key which was pressed. So, for example, you could display the code in the window status bar, a useful technique if you've forgotten which codes go with which keys.
<INPUT onKeyPress="window.status = getkey(event)">
This gives us this form:Most of the time you'll want to use
getkey()so make sure you copy both of them into your page.
goodchars() takes two arguments: the
event object and a string of acceptable characters. So, for example, we could allow only numbers in the field with code like this:
<INPUT NAME=INT onKeyPress="return goodchars(event,'0123456789')">
Which gives us this form:Be sure to put the word
returnbefore the call to
goodchars()or the event won't be cancelled if the user presses a wrong key.
goodchars()is case-insensitive. If you allow "X" you also automatically allow "x".
goodchars()allows the standard editing keys such as backspace and delete. Copyright 1997-2002 Idocs Inc. Content in this guide is offered freely to the public under the terms of the Open Content License and the Open Publication License. Contents may be redistributed or republished freely under these terms so long as credit to the original creator and contributors is maintained.