|
-
Jan 3rd, 2002, 04:15 AM
#1
Thread Starter
Evil Genius
JS one (gulp) regular expression Q ...
Code:
var blnRegExpValidEMail = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
So here I am, just downloaded a working example of a JS e-mail validation code & pinching it's code to shove into my project 
Thought it best to do some comments for once & explain what on Earth I'm trying to do incase I come back to it later, so I'm swamped under my javascript books here trying to make some sense of what the above's trying!
I've got :
- "/^[a-zA-Z0-9._-]" Is checking for any character apart from those in the square brackets.
- "+@" Checks that after the above characters have been removed, we've got 1 or more @ symbols in the string.
Why I've got brackets in the next bit, or the "+\" bit I'm not 100% on, I've found that the $ is searching for a piece of text at the end of the string, buit again, not sure what the "{2,4}" bits for ?!?!?!?!?
Can anyone help explain the "([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/" bit please ?
-
Jan 3rd, 2002, 09:23 AM
#2
Hyperactive Member
cry's out loud 
Iv'e just done you a nice well commented reply, but then I was testing something and closed IE by mistake. *{5}
Anyway that regular expression you had can be simplified into the one below, which will do the job of email validation no problem!
Code:
<script>
//match (.+) one or more occurences of any character except a new line followed by @ (@) followed by (.+) one or more occurences of any charachter except a new line followed by a literal . (\.)
var blnRegExpValidEMail = /.+@.+\./;
var test = prompt("email please");
var validate = test.match(blnRegExpValidEMail);
if (validate != null){
alert("email valid");
}else{
alert ("oh dear that was incorrect");
}
</script>
-
Jan 3rd, 2002, 09:28 AM
#3
Frenzied Member
Hmmmm... this shouldn't be a problem. For really good RegEx help, there are dedicated books. I don't like the RegEx section in the Rhino.
Code:
/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/
Okay, the caret (^) literally means, "match at begining of expression". So "^[a-zA-Z0-9._-]+" is looking for one or more characters at the begining of the string. It is looking for all lower and upper case roman characters and the 10 arabic numerals, in addition to looking for the underscore, the hyphen and ".", which translates to "any character except newline.".
I would really drop the dot and replace most of that with "\w", which is equivalent to "a-zA-Z0-9_".
Since it is trying to parse an email address, there should be a single at character. And that character should immediately follow the alphanumeric string at the beginning of the expression. The plus goes with the bracketed expression, not the @. It is looking for a single @.
The next part, "([a-zA-Z0-9.-]+\.)+" is looking for one or more of alphanumeric chains with, each followed by a dot. So if your email address is [email protected], or [email protected], it will match. The paraenthesis group the stuff within, and it augemented by the plus. The plus means, "match the previous one or more times."
I would widdle that expression down a bit, too.
Now the last bit, "[a-zA-Z0-9.-]{2,4}$". The dollar sign means "match this only at the end of the string". The {2,4} mean "match the previous atleast 2 times, but no more than 4 times". It is looking for a to character or four character TLD at the end of the email address. Really, you can drop the numeric and hyphen search here, and just look for letters.
Code:
/^[\w-]+@([\w-]+\.)+[a-zA-Z]{2,4}$/
I hope that makes sense. I tried to explain it all without boring you, but if I've left out anything, let me know.
Travis, Kung Foo Journeyman
As always, RTFM.
WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
YBMS, but Mozilla doesn't.
-
Jan 3rd, 2002, 09:31 AM
#4
Frenzied Member
Took me so long to post, that someone beat me to it.
But, Progressive, the following email address will break your expression thrice:
Travis, Kung Foo Journeyman
As always, RTFM.
WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
YBMS, but Mozilla doesn't.
-
Jan 3rd, 2002, 09:36 AM
#5
Hyperactive Member
no it wont!
so long as the @ is followed any character and then a fullstop it will be happy so it will gladly accept.
[email protected]dom.t9dgibberish
these are the only things you can be sure of in an email address it will contain a @ and a . so checking for anything else is pointless like checkking the number of occurences of characters and .
-
Jan 3rd, 2002, 09:38 AM
#6
Thread Starter
Evil Genius
thanks guys, this helps a lot !
-
Jan 3rd, 2002, 09:42 AM
#7
Hyperactive Member
no problem!
regular expressions can be a pain but are extemely useful!
-
Jan 3rd, 2002, 09:46 AM
#8
Frenzied Member
Originally posted by progressive
no it wont!
so long as the @ is followed any character and then a fullstop it will be happy  so it will gladly accept.
[email protected]dom.t9dgibberish
these are the only things you can be sure of in an email address it will contain a @ and a . so checking for anything else is pointless like checkking the number of occurences of characters and .
Uhm... you are contaminating Alex's mind.
First, the address I provided started at with an "f". It contained a tab character. Tabs are not allowed in email addresses. But, your RegEx happily matches tabs.
Second, TLDs do not have numbers in them. But your RegEx happily matches "t9d".
Third, the word "gibberish" was accidentally appended to an otherwise malformed address. Had it be appended to a legit address (such as [email protected]) it still would've been allowed to happily pass your RegEx.
Your expression lets entirely too much stuff in. You might as well let everything in and do no matching at all.
Travis, Kung Foo Journeyman
As always, RTFM.
WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
YBMS, but Mozilla doesn't.
-
Jan 3rd, 2002, 09:50 AM
#9
Thread Starter
Evil Genius
You've warped my fragile little mind !
Well I went with the one I had anyways, it don't matter too much but you both helped explain it to me (only got 1/2 of it from looking at the wrox books - left a chunk missing on this side of js )
-
Jan 3rd, 2002, 09:55 AM
#10
Frenzied Member
I somehow imagine "bob@bob/co.com" will pass the RegEx you had, even though it is not a valid email address.
Travis, Kung Foo Journeyman
As always, RTFM.
WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
YBMS, but Mozilla doesn't.
-
Jan 3rd, 2002, 10:00 AM
#11
Hyperactive Member
I'll give credit where credits due. Your right I'm wrong CyberTHuG.
At the end of the day though you can't stop a user from entering fraudulent email addresss. All you can do is make them think that you can, so checking for and @ symbol will make most people enter a correct address. When entering my email address in required fields i usually use.
[email protected]
hope thats not a real one as they'll be getting a lot of spam..lol
-
Jan 3rd, 2002, 12:55 PM
#12
Addicted Member
Originally posted by CiberTHuG
Code:
/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/
Now the last bit, "[a-zA-Z0-9.-]{2,4}$". The dollar sign means "match this only at the end of the string". The {2,4} mean "match the previous atleast 2 times, but no more than 4 times". It is looking for a to character or four character TLD at the end of the email address. Really, you can drop the numeric and hyphen search here, and just look for letters.
Code:
/^[\w-]+@([\w-]+\.)+[a-zA-Z]{2,4}$/
I hope that makes sense. I tried to explain it all without boring you, but if I've left out anything, let me know.
There's a minor flaw in what follows the @ in the original regexp.
The flaw would make @some_junk.com be illegal because _'s aren't included. Also, I like the CiberTHuG's [\w-] approach, but I'd include .'s too so: [\w-.] .
alex_read thought that ^[blah] meant that everything except b, l, a, h was accepted. That's an understandable mistake because the ^ has multiple uses (as do most of regexp's symbols) depending on context. [^blah] would match any character but b, l, a, h.
-
Jan 3rd, 2002, 01:21 PM
#13
Frenzied Member
According to the Rhino, "\w" is equvalent to "a-zA-Z0-9_".
I wouldn't use "." since it will match anything except a newline. It will match whitespace characters.
Travis, Kung Foo Journeyman
As always, RTFM.
WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
YBMS, but Mozilla doesn't.
-
Jan 3rd, 2002, 02:08 PM
#14
Addicted Member
I don't think that [\w-.] will match any character, If it does use
[\w-\.]
Otherwise you'll miss out on email addresses like [email protected]
cudabean
-
Jan 3rd, 2002, 02:49 PM
#15
Frenzied Member
Now that is a good point (and different from your previous one).
Code:
/^[\w-\.]+@([\w-]+\.)+[a-zA-Z]{2,4}$/
I think that works better.
For that matter, we can start testing to see if this would be better:
Code:
/^\S+@([\w-]+\.)+[a-zA-Z]{2,4}$/
Travis, Kung Foo Journeyman
As always, RTFM.
WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
YBMS, but Mozilla doesn't.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|