Solved

Regular expression: validate email

Posted on 2012-03-18
16
405 Views
Last Modified: 2012-06-21
I found this regualr expression to validate email but it accepts emails like this:

 
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"/>

Open in new window

123@4.3

Microsof's site has that : http://msdn.microsoft.com/en-us/library/aa479013.aspx
but 123@4.3 is not a valid email addess....


I need valid email addresses like test_testing@test.com or .gov, etc


Tried these 2 as well but they didnt accept examples like this :something@test.com.

"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"

--and this one

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

Open in new window

0
Comment
Question by:Camillia
  • 7
  • 4
  • 4
  • +1
16 Comments
 
LVL 3

Expert Comment

by:WebF00L
Comment Utility
Hi,


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

Open in new window

A try

http://www.regular-expressions.info/regexbuddy/email.html

//WebFooL Untangle Evangelist
0
 
LVL 20

Expert Comment

by:BuggyCoder
Comment Utility
try this:-

^((?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angle)>)$

Open in new window


http://regexlib.com/DisplayPatterns.aspx?AspxAutoDetectCookieSupport=1
http://www.codeproject.com/Articles/22777/Email-Address-Validation-Using-Regular-Expression
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
This one, as I posted, doesnt work. For example, I try test@i.com. It gets rejected by this

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

Open in new window


The one posted by BuggyCoder doesnt work. I plug it in ASP.Net aspx page and there are characters that throw errors.

Should I be using the one Microsoft is using?
0
 
LVL 20

Expert Comment

by:BuggyCoder
Comment Utility
well well well, if you are using it in aspx page then do something like this:-

 public const string MatchEmailPattern =
                  @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
     + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
                        [0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
     + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
                        [0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
     + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";

just add @ before string, i hope you are using c#.
Try visiting this link:-
http://www.codeproject.com/Articles/22777/Email-Address-Validation-Using-Regular-Expression
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
i am using C# but what you have throws errors. This is what i have now:

<asp:RegularExpressionValidator  ID="RegularExpressionValidator1" runat="server"  
                                      
                                     Text="Invalid email address!"
                                    ErrorMessage="Invalid email address!" 
                                    ControlToValidate="txtEmail"     
                                    ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"/>

Open in new window


Yes, i saw that link as well. But there has a be a better way of doing this. How's the performance of using such a long regualr expression??
0
 
LVL 20

Expert Comment

by:BuggyCoder
Comment Utility
this is the one i found on MSDN:-

^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$

try to add escape characters when you wrap it in a string variable on in control parameter........ or simply replace  " with '(single quote)
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
I changed double quotes to single but get syntax error. Not sure what you mean by escape characters...


 ValidationExpression="^(?('')(''.+?''@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"/>

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
You might find this article interesting: http://www.regular-expressions.info/email.html

It's the same site as WebF00L's link, but with explanations.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 20

Expert Comment

by:BuggyCoder
Comment Utility
here have a look at what are escape sequences or characters:-
http://msdn.microsoft.com/en-us/library/ms228362.aspx
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
you think i should use that one? test@i.com is not valid probably because of "i.com"....that probably below doesnt allow:

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

Open in new window


Or should I stick with what MS has but it allows stuff like 123@4.3

ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"/>

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
Maybe you should ask yourself what are the consequences of an invalid email address? If you have users setting up an account of some sort, it's a common technique to send an email to them to validate it's really them. Otherwise even a valid email address may not have the desired person at the other end.
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
Terry, you think i should stick with what MS has in their site?
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"/>

We won't use the email to send validation emails. It's just to have the user's email address. I think it would be ok to keep the same regualr expression that MS has, correct?
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
Comment Utility
Unless the ^ and $ are already applied automatically, you'd probably want:

ValidationExpression="^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"/>

Whether or not it works for you will depend on the consequences of a valid email address being rejected. I've seen users with a surname like O'Leary who use the apostrophe in their email address - technically it's valid, but your pattern doesn't allow it. Personally, I'd tend towards being too lenient rather than too strict.

If you wanted to relax your pattern, you could use something like:
ValidationExpression="^[^\s+.@-]+([-+.][^\s+.@-]+)*@[^\s+.@-]+([-.][^\s+.@-]+)*\.[^\s+.@-]+([-.][^\s+.@-]+)*$"/>

I've replaced all the occurrences of \w with [^\s+.@-]
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
let me try that.
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
The latest one accepts 123@4.5  and I'm assuming it's ok because I want it to be flexible. Correct?
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
Yes.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now