Home
Do you use a regular expression to validate an email address?
  v1.0 Posted at 26/04/2018 9:18 AM by Tiago Araujo

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​​

Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address:

    Comments:

    Note: Social Media login for Yotpo is not working in IE or Safari, please use Chrome. We are waiting for Yotpo to fix it.