Solved

ASP: need regexp for various email formats

Posted on 2014-04-25
17
393 Views
Last Modified: 2014-04-28
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(args.Value))

And advise of a regexp that would accept various email types.
0
Comment
Question by:badtz7229
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
17 Comments
 
LVL 33

Expert Comment

by:Big Monty
ID: 40023516
try this:

if( "^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$" )

taken from here:

http://www.markussipila.info/pub/emailvalidator.php?action=validate
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40023523
Why not go by what is mentioned here:  http://www.regular-expressions.info/email.html

\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

Open in new window


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.
0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 40023524
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/
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:badtz7229
ID: 40023552
@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)
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40023569
Turn on case-insensitivity:

/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i

Open in new window


Note the trailing "i" on the above expression.

e.g.

http://jsfiddle.net/d89gZ/
0
 

Author Comment

by:badtz7229
ID: 40023624
@kaufmed- that still didn't work. not even simple email addresses are accepted.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40023642
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/
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40023654
One last update:

http://jsfiddle.net/d89gZ/2/

You'll probably want to change the TLD check to be unbounded since you have "moneys" as one TLD. I've also switched the anchors from word boundaries ( \b ) to start of line ( ^ ) and end of line ( $ ), since it seems as though the entire string you are testing is in the email address (and it is not a substring of a larger string).

if (/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(args.Value)) {

Open in new window

0
 

Author Closing Comment

by:badtz7229
ID: 40023670
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.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40023737
^               - 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

Open in new window


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.
0
 

Author Comment

by:badtz7229
ID: 40023861
Thank u for explanation
0
 

Author Comment

by:badtz7229
ID: 40027423
could i have also used
 if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/.test(args.Value)) {

seems to be this works just as well?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40027440
\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)) {

Open in new window

0
 

Author Comment

by:badtz7229
ID: 40027993
thanks
0
 

Author Comment

by:badtz7229
ID: 40028025
@ kaufmed:
the only problem i find with
if (/^[\w.%+-]+@[\w.-]+\.[A-Z]{2,}$/i.test(args.Value)) {

is that this allows for multiple ".", so test...atol@aol.com is ok

whereas
 if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/.test(args.Value)) {
will catch that bc of  [\.-]?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40028129
For that reason, yes I agree. I was focused on the usage of \w, not the semantics of the overall pattern.
0
 

Author Comment

by:badtz7229
ID: 40028320
Thanks again.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question