frozentree Posted April 22, 2006 Share Posted April 22, 2006 i find it difficult to understand this tutorial on events.<html><body><script type="text/javascript">function noNumbers(e){var keynumvar keycharvar numcheckif(window.event) // IE{keynum = e.keyCode}else if(e.which) // Netscape/Firefox/Opera{keynum = e.which}keychar = String.fromCharCode(keynum)numcheck = /\d/return !numcheck.test(keychar)}</script><form><input type="text" onkeydown="return noNumbers(event)" /></form></html>the line " numcheck=/ \ d / " got me spinning in the head. " / \ d / " mean what?? please guide me ....... " d" means date?? or data? Link to comment Share on other sites More sharing options...
Webworldx Posted April 22, 2006 Share Posted April 22, 2006 It's RegExp, looking for a number, which is : \d in the syntax. The slashes before and after are the start and end of the RegExp.I prefer it written as:numcheck = new RegExp(/\d/); Link to comment Share on other sites More sharing options...
raimo Posted April 23, 2006 Share Posted April 23, 2006 If I was You I would filter out the numbers like this: function noNumbers(e){try{var key = (e.which) ? e.which : event.keyCode} catch(err){}return ((key == 8 || key > 31) && (key < 48 || key > 57));} Just added try/catch block when I found that Linux has some odd event with numpad keys.(some event causes undefined error)Please use onkeypress, it works much better than onkeydown:<input type="text" onkeypress="return noNumbers(event)" />edit:sorry, You are reading tutorial of events, not filttering numbers? Anyway, in this (my private) tutorial we learn how to play with events and errors and etc. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now