Regex Email validation

后端 未结 30 1219
北海茫月
北海茫月 2020-11-22 12:16

I use this

@\"^([\\w\\.\\-]+)@([\\w\\-]+)((\\.(\\w){2,3})+)$\"

regexp to validate the email

([\\w\\.\\-]+) - this is f

30条回答
  •  余生分开走
    2020-11-22 12:42

    This is my favorite approach to this so far:

    public static class CommonExtensions
    {
        public static bool IsValidEmail(this string thisEmail)
            => !string.IsNullOrWhiteSpace(thisEmail) &&
               new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$").IsMatch(thisEmail);
    }
    

    Then use the created string extension like:

    if (!emailAsString.IsValidEmail()) throw new Exception("Invalid Email");
    

提交回复
热议问题