Skip to content

Instantly share code, notes, and snippets.

@alassek
Created June 3, 2009 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alassek/123048 to your computer and use it in GitHub Desktop.
Save alassek/123048 to your computer and use it in GitHub Desktop.
private bool ValidEmailAddress(string emailAddress)
{
string qtext = "[^\\x0d\\x22\\x5c\\x80-\\xff]"; // <any CHAR excepting <">, "\" & CR, and including linear-white-space>
string dtext = "[^\\x0d\\x5b-\\x5d\\x80-\\xff]"; // <any CHAR excluding "[", "]", "\" & CR, & including linear-white-space>
string atom = "[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+"; // *<any CHAR except specials, SPACE and CTLs>
string quoted_pair = "\\x5c[\\x00-\\x7f]"; // "\" CHAR
string quoted_string = string.Format("\\x22({0}|{1})*\\x22", qtext, quoted_pair); // <"> *(qtext/quoted-pair) <">
string word = string.Format("({0}|{1})", atom, quoted_string); //atom / quoted-string
string domain_literal = string.Format("\\x5b({0}|{1})*\\x5d", dtext, quoted_pair); // "[" *(dtext / quoted-pair) "]"
string domain_ref = atom; // atom
string sub_domain = string.Format("({0}|{1})", domain_ref, domain_literal); // domain-ref / domain-literal
string domain = string.Format("{0}(\\x2e{0})*", sub_domain); // sub-domain *("." sub-domain)
string local_part = string.Format("{0}(\\x2e{0})*", word); // word *("." word)
string addr_spec = string.Format("{0}\\x40{1}", local_part, domain); //local-part "@" domain
Regex emailRegex = new Regex(string.Format("^{0}$", addr_spec));
Regex hostRegex = new Regex("^([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}$");
string host = emailAddress.Substring(emailAddress.LastIndexOf('@') + 1);
return emailRegex.IsMatch(emailAddress) & hostRegex.IsMatch(host);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment