...

View Full Version : Validating an Email Address



Fun Leprechaun
04-15-2009, 11:39 PM
Hi there,

Can someone explain this piece of code to me? I get the general idea here but can you break it down for me as I don't just want to copy and paste it without fully understanding it!

Thank you so much in advance! :D

Here it is:


/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,5}$/i

Fun Leprechaun
04-15-2009, 11:43 PM
Also,

Can you tell me what this means?

/^\s*$/

bazz
04-16-2009, 12:48 AM
OK, its hard to work out which is the best way to explain in this medium but I'll have a go.

I'll work from left to right.

ok, then;

the / is the first boundary of the regex.
the ^ means the beginning of your regex manipulation
the first [] section shows what chars are allowed in the first part of the email address.
the + means that each of those chars can occur more than once
the @ symbol represents the @ in the address
the second [] section shows the chars allowed after the @ symbol and before the .tld
again, the + means more than one occurence is accepted
\ means escape the .
[A-Z] allows for all alphabetical chars for the domain suffix (tld)
{2,5} means minimum of 2 max of 5 chars in the tld
$ means the end of your manipulation
/ denotes the end of the regex and
i means case insensitive

It should be noted that using the ^ and $ means you are checking the complete snippet. if you just use // and no ^ or $, then you are chekcing for a match anywhere, whereas the ^ and $ means that the match has to match the whole thing from start to end. I'm sure there is an easier way to explain that but I hope that helps

the '5' needs to be changed to about 7 since tld's can have more than 5 letters. not sure the max number though. Please note I am not evaluating whther that is in fact a good regex for your purpose. O'Reilly does a good book on the subject (and others) at your fav online bookshop.

ps, are you from Emerald Isle?
bazz

Apostropartheid
04-16-2009, 04:17 AM
Not really a JavaScript man, but wouldn't that fail when presented with emails whose issuing website is registered under a third-level domain (e.g. *.co.uk)?

TinyScript
04-16-2009, 04:24 AM
OK, its hard to work out which is the best way to explain in this medium but I'll have a go.

I'll work from left to right.

ok, then;

the / is the first boundary of the regex.
the ^ means the beginning of your regex manipulation
the first [] section shows what chars are allowed in the first part of the email address.
the + means that each of those chars can occur more than once
the @ symbol represents the @ in the address
the second [] section shows the chars allowed after the @ symbol and before the .tld
again, the + means more than one occurence is accepted
\ means escape the .
[A-Z] allows for all alphabetical chars for the domain suffix (tld)
{2,5} means minimum of 2 max of 5 chars in the tld
$ means the end of your manipulation
/ denotes the end of the regex and
i means case insensitive

It should be noted that using the ^ and $ means you are checking the complete snippet. if you just use // and no ^ or $, then you are chekcing for a match anywhere, whereas the ^ and $ means that the match has to match the whole thing from start to end. I'm sure there is an easier way to explain that but I hope that helps

the '5' needs to be changed to about 7 since tld's can have more than 5 letters. not sure the max number though. Please note I am not evaluating whther that is in fact a good regex for your purpose. O'Reilly does a good book on the subject (and others) at your fav online bookshop.

ps, are you from Emerald Isle?
bazz

Can I save that on a web page for easy reference? I just wanted to ask first in case you wanted to make tutorial page. That's a sweet explanation

bazz
04-16-2009, 04:36 AM
Not really a JavaScript man, but wouldn't that fail when presented with emails whose issuing website is registered under a third-level domain (e.g. *.co.uk)?

yeh I think it would.

I doubt that it is a very good regex. I would need to hoke out the O'reilly book but I seem to recall the example they gave is much longer and more difficult to read.


@TinyScript: I don't see a problem with you copying it. No need to attribute it to me either.

bazz

Philip M
04-19-2009, 08:36 PM
I find the best email regex is:-

if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(eMailAddress.value))) {


Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.

bazz
04-20-2009, 12:52 AM
yeh, Philip, thats more like the O'Reilly one I referred to though I can't place the book. :(

re. your sig.



Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.

:D:D I can relate to that :)

bazz



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum