tesmc
asked on
ASP: need regexp for various email formats
I need a regexp that allows emails as follows: name.tac@fr.mac.moneys
In addition to regular emails like test@aol.com
I found the following regexp, but it doesn't satisfy emails where more than 1 period.
Can you please explain what this current regexp is doing?
if (/^\w+([\.-]?\w+)*@\w+([\. -]?\w+)*(\ .\w{2,3})+ $/.test(ar gs.Value))
And advise of a regexp that would accept various email types.
In addition to regular emails like test@aol.com
I found the following regexp, but it doesn't satisfy emails where more than 1 period.
Can you please explain what this current regexp is doing?
if (/^\w+([\.-]?\w+)*@\w+([\.
And advise of a regexp that would accept various email types.
Why not go by what is mentioned here: http://www.regular-expressions.info/email.html
It's a bit generic, but I tend to agree with the author's logic regarding complex email validation. Besides, a regex can't tell you if the email address actually exists--only the receiving server can do that.
\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b
It's a bit generic, but I tend to agree with the author's logic regarding complex email validation. Besides, a regex can't tell you if the email address actually exists--only the receiving server can do that.
RegEx always takes me some time. For validating emails I have just been checking for the @ and length greater than 3 with something on both sides of the @. There are a lot of new gtld's coming on line this year to watch for.
http://www.regular-expressions.info/email.html
New gtld's http://www.newtldlist.com/
http://www.regular-expressions.info/email.html
New gtld's http://www.newtldlist.com/
ASKER
@Big Monty: this example wouldn't work on name.tac@fr.mac.moneys but it did work for name.tac@fr.mac.mone
@kaufmed: your solution unfortunately, did not work. not even for simple emails like test@aol.com (weird)
@kaufmed: your solution unfortunately, did not work. not even for simple emails like test@aol.com (weird)
Turn on case-insensitivity:
Note the trailing "i" on the above expression.
e.g.
http://jsfiddle.net/d89gZ/
/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i
Note the trailing "i" on the above expression.
e.g.
http://jsfiddle.net/d89gZ/
ASKER
@kaufmed- that still didn't work. not even simple email addresses are accepted.
Did you check my sample code on JSFiddle? Every example you've given thus far validates in the sample. Updated to add additional prompts:
http://jsfiddle.net/d89gZ/1/
http://jsfiddle.net/d89gZ/1/
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
yes this worked for the types of emails i'm dealing with. thanks.
can you please explain what this regexp does and during what conditions it would fail?
i'm new to this.
can you please explain what this regexp does and during what conditions it would fail?
i'm new to this.
^ - Matches start of string
[A-Z0-9._%+-]+ - Matches one or more ( + ) of any ( [...] ) letter ( A-Z ), number ( 0-9 ), period/full stop, underscore, percent, plus, or hypen ( ._%+- )
@ - Matches literal @
[A-Z0-9.-]+ - Matches one or more ( + ) of any ( [...] ) letter ( A-Z ), number ( 0-9 ), period/full stop ( . ), or hyphen ( - )
\. - Matches literal period/full stop
[A-Z]{2,} - Matches at least two ( {2,} ) of any ( [...] ) letter ( A-Z )
$ - Matches end of string
i - Turns on case-insensitivity
The net effect is that you find one or more "words" each separated by a period/full stop (if present), then an @ symbol, then one or more "words" each separated by a period/full stop (if present), then the last period/full stop, then the TLD. The TLD must be at least two characters (e.g. .co, .us, .jp, etc.).
As far as when it "fails," I take it you mean what inputs will it reject? Anything with two @ symbols will be rejected. If the part trailing the @ does not have at least one dot, then that will be rejected. Anything with special characters other than period/full stop, underscore, percent, plus, or hyphen (e.g. $, ^, &, etc. ) will be rejected. (To be fair, & is actually a valid character in email addresses, as are many others. It is in less common usage, though.) If the TLD contains numbers, then it will be rejected.
ASKER
Thank u for explanation
ASKER
could i have also used
if (/^\w+([\.-]?\w+)*@\w+([\. -]?\w+)*(\ .\w{2,})+$ /.test(arg s.Value)) {
seems to be this works just as well?
if (/^\w+([\.-]?\w+)*@\w+([\.
seems to be this works just as well?
\w is shorthand for [a-zA-Z0-9_]. The equivalent to the pattern I suggested would be:
if (/^[\w.%+-]+@[\w.-]+\.[A-Z]{2,}$/i.test(args.Value)) {
ASKER
thanks
ASKER
@ kaufmed:
the only problem i find with
if (/^[\w.%+-]+@[\w.-]+\.[A-Z ]{2,}$/i.t est(args.V alue)) {
is that this allows for multiple ".", so test...atol@aol.com is ok
whereas
if (/^\w+([\.-]?\w+)*@\w+([\. -]?\w+)*(\ .\w{2,})+$ /.test(arg s.Value)) {
will catch that bc of [\.-]?
the only problem i find with
if (/^[\w.%+-]+@[\w.-]+\.[A-Z
is that this allows for multiple ".", so test...atol@aol.com is ok
whereas
if (/^\w+([\.-]?\w+)*@\w+([\.
will catch that bc of [\.-]?
For that reason, yes I agree. I was focused on the usage of \w, not the semantics of the overall pattern.
ASKER
Thanks again.
if( "^[a-z0-9_\+-]+(\.[a-z0-9_
taken from here:
http://www.markussipila.info/pub/emailvalidator.php?action=validate