Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regular expression: validate email

Posted on 2012-03-18
16
Medium Priority
?
426 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
CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

 
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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

670 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