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

Updated by Brook Jeynes [SSW] 1 year ago. See history

123

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: 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: Figure: Good example of verify email address

acknowledgements
related rules