Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <remarks>No special characters Space and "(),:;<>@[\] are allowed, though valid by standard.</remarks>
- public static bool IsValidEmail(string email)
- {
- if (string.IsNullOrWhiteSpace(email)) return false;
- string LocalName = "";
- int state = 0;
- foreach(var c in email)
- switch (state) {
- case 0:// wait for the first valid character of mailbox
- if (!IsValidEmailChar(c)) return false;
- LocalName += c;
- state = 1;
- break;
- case 1:// accumulate mailbox string or jump to domain by @
- if (c == '@') {
- // validate mailbox on special case of "."
- if (LocalName.StartsWith(".") || LocalName.EndsWith(".") || LocalName.Contains("..")) return false;
- state = 2;
- break;
- }
- if (!IsValidEmailChar(c)) return false;
- LocalName += c;
- break;
- case 2:// wait for any valid DNS character
- if (!IsValidDNSChar(c)) return false;
- state = 3;
- break;
- case 3:
- if (c == '.')
- state = 2;
- else
- if (!IsValidDNSChar(c)) return false;
- break;
- }
- return state == 3;
- }
Add Comment
Please, Sign In to add comment