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

Loading last updated info...

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

Authors

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.

We open source.Loving SSW Rules? Star us on GitHub. Star