This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static bool ValidateEmailAddress(string email) | |
{ | |
if (email.Length > 254) | |
return false; | |
else | |
{ | |
string[] emailParts = email.Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries); | |
if (emailParts.Length != 2 || email.StartsWith("@") || email.EndsWith("@") || (new Regex("[@]{2,}", RegexOptions.None)).IsMatch(email)) | |
return false; | |
else | |
{ | |
if (emailParts[0].Length <= 64) | |
{ | |
// alias | |
Regex rx1 = new Regex(@"^[a-zA-Z0-9_-][a-zA-Z0-9_\.-]*[a-zA-Z0-9_-]$", RegexOptions.None); | |
if (emailParts[0].Length > 64 || emailParts[0].Contains("..") || !rx1.IsMatch(emailParts[0])) | |
return false; | |
} | |
else | |
return false; | |
// domain | |
Regex rx2 = new Regex(@"^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\.[a-zA-Z]{2,63}$", RegexOptions.None); | |
if (emailParts[1].Contains("--") || !rx2.IsMatch(emailParts[1])) | |
return false; | |
} | |
} | |
return true; | |
} |
No comments:
Post a Comment