Wednesday, June 11, 2008

Validate an email address using Regular expressions.

This sample code snippet helps to validate an email address using regular expressions. Returns true is the address provided is true, otherwise returns false
public bool IsValidEmail(string email)
{
string pat = @"^[-a-zA-Z0-9][-.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\.(comeduinfogovintmilnetorgbiznamemuseumcoopaeroprotv[a-zA-Z]{2})$";
Regex check = new Regex(pat,RegexOptions.IgnorePatternWhitespace);
bool valid = false;
if (string.IsNullOrEmpty(email))
{
valid = false;
}
else
{
valid = check.IsMatch(email);
}
return valid;
}

No comments: