Solved

Regular expression: validate email

Posted on 2012-03-18
16
421 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
Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

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