Regex Email validation

后端 未结 30 904
北海茫月
北海茫月 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:47

    Email validation using regex

        string pattern = @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z";
    
        //check first string
       if (Regex.IsMatch(EmailId1 , pattern))
       {    
           //if email is valid
            Console.WriteLine(EmailId1+ " is a valid Email address ");
       }
    

    Source: email validation c#

    Validation Without Regex using MailAddress.MailAddress(String) class constructor

    public bool IsEmailValid(string emailaddress)
    {
     try
     {
        MailAddress m = new MailAddress(emailaddress);
        return true;
     }
     catch (FormatException)
     {
        return false;
     }
    }
    

提交回复
热议问题