Regular expression: validate email

I found this regualr expression to validate email but it accepts emails like this:


Open in new window


Microsof's site has that :
but 123@4.3 is not a valid email addess....

I need valid email addresses like or .gov, etc

Tried these 2 as well but they didnt accept examples like this


--and this one


Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.



Open in new window

A try

//WebFooL Untangle Evangelist
try this:-


Open in new window
CamilliaAuthor Commented:
This one, as I posted, doesnt work. For example, I try It gets rejected by this


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?
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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

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

just add @ before string, i hope you are using c#.
Try visiting this link:-
CamilliaAuthor Commented:
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!" 

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??
this is the one i found on MSDN:-


try to add escape characters when you wrap it in a string variable on in control parameter........ or simply replace  " with '(single quote)
CamilliaAuthor Commented:
I changed double quotes to single but get syntax error. Not sure what you mean by escape characters...


Open in new window

Terry WoodsIT GuruCommented:
You might find this article interesting:

It's the same site as WebF00L's link, but with explanations.
here have a look at what are escape sequences or characters:-
CamilliaAuthor Commented:
you think i should use that one? is not valid probably because of ""....that probably below doesnt allow:


Open in new window

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


Open in new window

Terry WoodsIT GuruCommented:
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.
CamilliaAuthor Commented:
Terry, you think i should stick with what MS has in their site?

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?
Terry WoodsIT GuruCommented:
Unless the ^ and $ are already applied automatically, you'd probably want:


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:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CamilliaAuthor Commented:
let me try that.
CamilliaAuthor Commented:
The latest one accepts 123@4.5  and I'm assuming it's ok because I want it to be flexible. Correct?
Terry WoodsIT GuruCommented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.