Click to See Complete Forum and Search --> : Can anyone explain this please


kimoo
June 30th, 2007, 12:45 PM
Hello,


function noNumbers(e)
{
var keynum
var keychar
var numcheck

if(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>



i can understand the whole code except the last 2 lines


numcheck = /\d/
return !numcheck.test(keychar)


what does /\d/ mean and what is !numcheck.text(keychar).. are these some predefined JavaScript stuff..?

any body with an expalination please as this is makiing me crazy

kimoo

PeejAvery
June 30th, 2007, 01:10 PM
That is called regular expressions. \d means digit. The test function is checking to see if there is a digit in the variable keychar. Now, the ! changes it to check for a falsey statement.

So, in short it checks for a digit inside the variable keychar.

kimoo
July 1st, 2007, 06:43 AM
oh okay!

if /d is digit then whats the alphabet? if there is any


and where did the test function come from?


thanks
kimoo

42_south
July 1st, 2007, 06:58 AM
Regular expressions are text patterns that are used for string matching. Regular expressions are strings that contains a mix of plain text and special characters to indicate what kind of matching to do. Here's a very brief turorial on using regular expressions... codeguru.com ... /regex/article (http://www.codeguru.com/cpp/cpp/string/regex/article.php/c2791/#more)

kimoo
July 1st, 2007, 07:51 AM
thanks alot very informative

kimoo

kimoo
July 1st, 2007, 08:01 AM
this one is very useful and it also explains the .test

.test is a method in the regexp

http://www.devarticles.com/c/a/JavaScript/Regular-expressions-in-JavaScript/1/

kimoo

PeejAvery
July 1st, 2007, 09:02 AM
.test is a method in the regexp
Now you got it. :wave: