-
JavaScript syntax
OK, so I ran across the following code (in someone's webpage) that checks whether an email address submitted is actually valid (in syntax only, my guess).
Code:
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function checkEmail(myForm) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.email.value)){
return (true)
}
alert("Invalid E-mail Address! Please re-enter.")
return (false)
}
// End -->
</script>
Could someone go through the line if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.email.value)) end explain the syntax (what checks are made)? For example, what does "/^\w" accomplish?
-
To a normal person I bet it looks like a lot of BS. :)
-
Looks like a regular expression. I know they exist, they have form like this and they are a lot used for pattern matching, but that's all I know about them.
Try searching for regular expressions in JavaScript using a search engine. A few sites I've found.
http://www.johnrobertmorris.com/dev/Regex.asp
http://www.edevcafe.com/viewdoc.php?eid=98
From the first site, / seems to be the start of the expression. Then ^ indicates that the pattern matching should start from the beginning, then \w indicates that any alphanumeric character is valid. So a valid e-mail address should always start with an alphanumeric character.
-
Basicly it says
match some ammount of letters, then a @ , then some other letters, then a ., then 3 more letters.
the"[/.w+]" type things are disqualifiers. Making sure there arnt illegal characters lie /, ^, $& or white spaces in the email address.
-
Code:
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
^ - matches the beginning of the line
\w+ - matches a word character 1 or more times
([\.-]? - matches . or - 0 or 1 times
\w+)* - matches any word character 1 or more times
The * matches the entire expression in parens 0 or more times
@ matches the @ character exactly once
\w+([\.-]?\w+)* - does exactly what it did before
(\.\w{2,3})+ - matches a . followed by 2 to 3 word characters 1 or more times
$ - matches the end of the line
-
so would that script be basically used for checking if the email adress is the same in two different text fields?
-
No, it just makes sure that the e-mail address follows a specific format. i.e.:
abc is not valid,
[email protected] is valid,
[email protected] is not,
[email protected] is, etc...
Its just for matching patterns in a string.
-
why is the matching code so confusing then? I've done javascript before, and actually something quite similar to the above code, but the syntax was a lot different.
-
You'll find regular expressions in many languages. JavaScript just happens to implement them. The thing is, most other ways of pattern matching are language or library specific. A regular expression is just a string which defines the pattern that you're looking for, and then whatever function gets passed the string (or however it is implemented) has to use its own language/library specific methods to find that pattern. In JavaScript, it appears that regular expressions are in fact objects in themselves. The syntax is a bit odd though (the /reg-exp/.test( ) thing looks a bit strange to me).
-
I took some time to study the code and your explanation of it, and I guess it does make some logical sense...yet I don't see writting it without a ready reference...even if one coded in JScript all the time, things of that nature would be impossible to remember.
-
what exactly do regular expressions offer, anyway? does it just save space/time when coding?
-
Yeah, saving time and space is one thing. Also, they are fairly standard. If you use them from time to time, it can be nice to actually write out the pattern instead of messing with functions and objects.
I certainly can't do too much with them without reference (I can do some basic things, but it always helps to have a table of special characters in front of me). The place I have seen them used most is in Perl code.
-
Thanks, that's exactly what I needed. I knew what the code did, just wanted to know what the syntax in the expression meant. Thanks :)