Guest User

Untitled

a guest
Jan 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1. /// <remarks>No special characters Space and "(),:;<>@[\] are allowed, though valid by standard.</remarks>
  2.         public static bool IsValidEmail(string email)
  3.         {
  4.             if (string.IsNullOrWhiteSpace(email)) return false;
  5.             string LocalName = "";
  6.             int state = 0;
  7.             foreach(var c in email)
  8.                 switch (state) {
  9.                 case 0:// wait for the first valid character of mailbox
  10.                     if (!IsValidEmailChar(c)) return false;
  11.                     LocalName += c;
  12.                     state = 1;
  13.                     break;
  14.                 case 1:// accumulate mailbox string or jump to domain by @
  15.                     if (c == '@') {
  16.                         // validate mailbox on special case of "."
  17.                         if (LocalName.StartsWith(".") || LocalName.EndsWith(".") || LocalName.Contains("..")) return false;
  18.  
  19.                         state = 2;
  20.                         break;
  21.                     }
  22.                     if (!IsValidEmailChar(c)) return false;
  23.                     LocalName += c;
  24.                     break;
  25.                 case 2:// wait for any valid DNS character
  26.                     if (!IsValidDNSChar(c)) return false;
  27.                     state = 3;
  28.                     break;
  29.                 case 3:
  30.                     if (c == '.')
  31.                         state = 2;
  32.                     else
  33.                         if (!IsValidDNSChar(c)) return false;
  34.                     break;
  35.                 }
  36.             return state == 3;
  37.         }
Add Comment
Please, Sign In to add comment