Validate email using regular expression in C#


Simple C# function to validate email address using regular expression.

Regular expression for email validation

private static readonly Regex emailRegex = new Regex(
            @"^[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$", RegexOptions.IgnoreCase);

Example of email validation


/// <summary>
/// Validates an email address.
/// </summary>
/// <param name="email"></param>
/// <returns></returns>
public static bool IsEmailValid(string email)
{
    if (!string.IsNullOrWhiteSpace(email))
    {
        return emailRegex.IsMatch(email.Trim());
    }

    return false;
}