[thelist] Why does this JS code work the way it does???

tim tim at hyperlinkage.com
Tue Dec 12 19:37:27 CST 2006

Anthony Baratta wrote:
>     var strValidKeys = "1234567890";
>     if (strValidKeys.indexOf(String.fromCharCode(event.keyCode)) == -1)

I desperately hope that you have a noscript and non-keyboard alternative 
to the above code listing.

Also, which keys are you hoping to catch?  indexOf looks like the wrong 
method to use for this.

 >     if ( ...stuff... == -1)
>         return false;
>     else
>         return true;


You've used four lines of code which could have simply been:

return ! (strValidKeys.indexOf(String.fromCharCode(event.keyCode)) == -1)


More information about the thelist mailing list