Regular Expression for Email Address validation in C#

By Ankit Goyal | June 6, 2009 | 10,313 views
Category .NET


About author  Working on .NET technologies for past 2yrs. I am very passionate about the new features which get introduced in the technology. I am here to share the new features i get to know in the world of Microsoft. Follow me on twitter @ankygoyal Read more from this author


The task at hand is to validate the Textbox in asp.net for a valid email address.

We can attach a RegularExpressionValidator to the text box with the following regular expression:

<asp:RegularExpressionValidator ValidationExpression="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$" ID="rxpEmail" runat="server"
ErrorMessage="Email Address is not valid" Text="*" Display="Static" ControlToValidate="txtEmail"></asp:RegularExpressionValidator>

UPDATES
Well i searched the net for the standards of a valid email id and i came across with a wonderful article at

I Knew How To Validate An Email Address Until I Read The RFC

It gives a an idea of what an exact RFC standard for the email id is.

I believe that we should follow validations which are quite obvious to the users and allow only standard email addresses.

kick it on DotNetKicks.com

Shout it

pimp it

Read more post on ASP.NET 2.0 C#.NET 2.0 Microsoft 

12 comments | Add One

Comments

  1. JLNo Gravatar - 06/6/2009 at 8:41 am

    I don’t understand the right part of the regex.
    It would match:
    name1.name2@mail.123

    I don’t think tld can be numbers.



  2. Ankit GoyalNo Gravatar - 06/7/2009 at 10:07 am

    i think we can have email ids of type
    abc@cmpnyname.local1



  3. DavidNo Gravatar - 06/7/2009 at 11:34 pm

    “+” is also valid character.

    e.g.

    yourname+thispartgetsignored@gmail.com

    is equivalent to:

    yourname@gmail.com



  4. Ankit GoyalNo Gravatar - 06/8/2009 at 12:29 am

    new info for me….
    have never tried this…i will work on it…
    thnkss david…



  5. Micah BurnettNo Gravatar - 06/8/2009 at 7:33 am

    I prefer a dumbed down the expression that catches most of the real-world problems instead of verifying every possible combination. The ultimate validation is when the mail server rejects the address…then you know for sure!

    Our Republic



  6. Ankit GoyalNo Gravatar - 06/8/2009 at 7:40 am

    Micah u r rite as the ultimate validation for a email address is achieved when we ping the mail server for the specified email address…



  7. johnNo Gravatar - 10/29/2009 at 12:37 pm

    Hi, I could see one nice method here http://www.Tctcworld.com. if u need go through it..



  8. CraigNo Gravatar - 11/6/2009 at 9:56 am

    If you want to validate e-mail addresses by pinging their mail server just use EmailVerify.NET, a .NET component which can test e-mail syntactically, verify the DNS MX records for the each domain and connect to each mail server to check if the mailbox exists or not.

    It works like a charm!!
    There’s also a nice demo on their website: http://www.emailverify.net

    Hope this helps.



  9. MargishNo Gravatar - 02/19/2010 at 8:00 am

    This worked for my task
    [a-z]*\.*[a-z]*@[a-z]*\.[a-z]*



Trackbacks

  1. DotNetShoutout
  2. DotNetBurner - C#
  3. PimpThisBlog.com

Leave a Comment

Name:

E-Mail :

Website :

Comments :