Hi Guys
Many thanks for your valued help.
I didn't manage to see these posts until today, so the method I'm currently
using is:
function CheckCodeEntry(e) {
var key = window.event ? e.keyCode : e.which;
if (e.shiftKey==1) {
if (key == 8 || key == 9 ||
key == 46 || (key > 36 && key < 41) ||
(key > 63 && key < 74) || (key > 64 && key < 91) ||
key == 189) {
return true;
}
else {
return false;
}
}
else {
if (key == 8 || key == 9 || key == 190 ||
key == 46 || (key > 36 && key < 41) ||
(key > 47 && key < 5

|| (key > 64 && key < 91) ||
(key > 95 && key < 106) || key == 189) {
return true;
}
else {
return false;
}
}
}
JS in input box is onKeyDown='return CheckCodeEntry(event)'
onKeydown='return CheckCodeEntry(event)'
Can you see any issues with this? Appears to work OK in IE and FireFox.
Please note that I only use this code as a helping hand to the user at the
point of entry. I also check on submitting the form (JS) and server side
(ASP).
Thanks
Laphan
"GreyWyvern" <spam.RemoveThis@greywyvern.com> wrote in message
news:op.tiq6rfb3sl6xfd@news.nas.net...
And lo, Laphan didst speak in alt.www.webmaster,comp.lang.javascript:
> Hi All
>
> I'm using the below to limit the input into a text box to just letters,
> numbers, hyphens and full stops, but I also need to allow the backspace,
> delete and arrow keys to come through. How can I do this?
>
> <script language="JavaScript" type="text/javascript">
> <!--
> function onKeyPressBlockNumbers(e)
> {
> var key = window.event ? e.keyCode : e.which;
> var keychar = String.fromCharCode(key);
> reg = /^[.a-zA-Z0-9_-]*$/;
> return reg.test(keychar);
> }
> //-->
> </script>
First, don't use the onkeypress event. onkeypress differs from
onkeydown/onkeyup in that the code it returns is mostly limited to the
ASCII set and a few special keys.
In contrast, onkeyup and onkeydown return codes which report almost every
key on the keyboard, from the letter A to the F1-F12 function keys. This
is made possible mostly due to discarding the lowercase letter range of
the ASCII set and using these values for various special keys. Because of
this, you cannot determine whether a letter entered is uppercase or
lowercase quite so easily, but usually this isn't a problem. I recommend
using the onkeydown event.
Second, don't use a regexp at all. Instead filter the input directly from
the keyCode.
Here are the relevant keyCodes you want:
Letters = 65 - 90
Numbers = 48 - 57
Hyphen = 189
Full Stop = 190
Backspace = 8
Delete = 46
Arrow Keys = 37 - 40
If the keyCode recieved does not match anything in those ranges, simply
disallow the event. Keep in mind that this security is worthless if JS is
disabled so be sure to do full checking server-side also.
Grey
--
The technical axiom that nothing is impossible sinisterly implies the
pitfall corollary that nothing is ridiculous.
-
http://www.greywyvern.com/orca#search - Orca Search: Full-featured
spider and site-search engine
>> Stay informed about: RegExp for delete, backspace and arrow keys