Thursday, February 04, 2016

Validate Email Address in C#

I Knew How To Validate An Email Address Until I Read The RFC - You've Been Haacked
at-sign
C# code to validate email address - Stack Overflow

using System.Text.RegularExpressions;
public static class EmailValidator {
    static Regex ValidEmailRegex = new Regex(
              @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
            + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
            + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$"
            , RegexOptions.IgnoreCase);
    }
    public static bool IsValid(string emailAddress) {
        return ValidEmailRegex.IsMatch(emailAddress);
    }
}

No comments: