Use regular expressions to validate an email in c# using Regex

Sunday, 4 October 2009 18:32 by myro

If you need a simple method that validates an email address in c#, you are at the right place. The code snipped illustrated below, will check if the provided email address matches a regular expressionto and will determine if is a valid email or not.

public bool IsMailValid(string emailAddress)
{
    StringBuilder sb = new StringBuilder();
    sb.Append(@"^(([^<>()[\]\\.,;:\s@\""]+");
    sb.Append(@"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@");
    sb.Append(@"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}");
    sb.Append(@"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+");
    sb.Append(@"[a-zA-Z]{2,}))$");
    Regex reStrict = new Regex(sb.ToString());
    bool isStrictMatch = reStrict.IsMatch(emailAddress);
    return isStrictMatch;
}

Use it, paste it and share it!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   , ,
Categories:   .NET
Actions:   Bookmark and Share | Permalink | Comments (0) | Comment RSSRSS comment feed
If you consider this post usefull for your purposes, please consider visiting my sponsors to help me out with the myrocode.com maintenance. Thank you.