Do you use a regular expression to validate an email address?

Last updated by Brook Jeynes [SSW] 11 months ago.See history

A regex is the best way to verify an email address.

public bool IsValidEmail(string email)
{
 // Return true if it is in valid email format.
 if (email.IndexOf("@") <= 0) return false;
 if (email.EndWith("@")) return false;
 if (email.IndexOf(".") <= 0) return false;
 if ( ...
}

Figure: Bad example of verify email address

public bool IsValidEmail(string email)
{
 // Return true if it is in valid email format.
 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})(\]?)$";
}

Figure: Good example of verify email address

Adam Cogan
We open source. Powered by GitHub