How to verify that string is in valid e-mail format

by admin 16. April 2008 22:35

The following code uses the static Regex.IsMatch method to verify that a string is in valid e-mail format.

The IsValidEmail method returns true if the string contains a valid e-mail address and false if it does not, but takes no other action.

You can use IsValidEmail to filter out e-mail addresses containing invalid characters before your application stores the addresses in a database or displays them in an ASP.NET page.

bool IsValidEmail(string email)
{

     return System.Text.RegularExpressions.Regex.IsMatch(email, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

asp.net | c# | Regex

Comments are closed

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

Calendar

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar