Validator.cs 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. namespace MediaBrowser.Server.Implementations.Connect
  8. {
  9. public static class Validator
  10. {
  11. static Regex ValidEmailRegex = CreateValidEmailRegex();
  12. /// <summary>
  13. /// Taken from http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx
  14. /// </summary>
  15. /// <returns></returns>
  16. private static Regex CreateValidEmailRegex()
  17. {
  18. string validEmailPattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
  19. + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
  20. + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";
  21. return new Regex(validEmailPattern, RegexOptions.IgnoreCase);
  22. }
  23. internal static bool EmailIsValid(string emailAddress)
  24. {
  25. bool isValid = ValidEmailRegex.IsMatch(emailAddress);
  26. return isValid;
  27. }
  28. }
  29. }