Solved

Regular expression: validate email

Posted on 2012-03-18
16
418 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
[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
  • 7
  • 4
  • 4
  • +1
16 Comments
 
LVL 3

Expert Comment

by:WebF00L
ID: 37735106
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
ID: 37735147
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
ID: 37735289
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37735305
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
ID: 37735323
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
ID: 37735342
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
ID: 37735455
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
ID: 37735458
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
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37735472
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
ID: 37735485
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
ID: 37735494
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
ID: 37735809
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
ID: 37735911
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
ID: 37735968
let me try that.
0
 
LVL 7

Author Comment

by:Camillia
ID: 37736056
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
ID: 37736058
Yes.
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

729 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