Regex Email validation

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

    here is our Regex for this case:

    @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                           @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                           @".)+))([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$",
    

    there are three parts, which are checcked. the last one is propably the one you need. the specific term {2,6} indicates you the min/max length of the TLD at the end. HTH

    0 讨论(0)
  • 2020-11-22 12:25
    public static bool ValidateEmail(string str)
    {                       
         return Regex.IsMatch(str, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
    }
    

    I use the above code to validate the email address.

    0 讨论(0)
  • 2020-11-22 12:25
       public bool VailidateEntriesForAccount()
        {
           if (!(txtMailId.Text.Trim() == string.Empty))
            {
                if (!IsEmail(txtMailId.Text))
                {
                    Logger.Debug("Entered invalid Email ID's");
                    MessageBox.Show("Please enter valid Email Id's" );
                    txtMailId.Focus();
                    return false;
                }
            }
         }
       private bool IsEmail(string strEmail)
        {
            Regex validateEmail = new Regex("^[\\W]*([\\w+\\-.%]+@[\\w\\-.]+\\.[A-Za-z] {2,4}[\\W]*,{1}[\\W]*)*([\\w+\\-.%]+@[\\w\\-.]+\\.[A-Za-z]{2,4})[\\W]*$");
            return validateEmail.IsMatch(strEmail);
        }
    
    0 讨论(0)
  • 2020-11-22 12:28

    I think @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$" should work.
    You need to write it like

    string email = txtemail.Text;
    Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
    Match match = regex.Match(email);
    if (match.Success)
        Response.Write(email + " is correct");
    else
        Response.Write(email + " is incorrect");
    

    Be warned that this will fail if:

    1. There is a subdomain after the @ symbol.

    2. You use a TLD with a length greater than 3, such as .info

    0 讨论(0)
  • 2020-11-22 12:29

    Try the Following Code:

    using System.Text.RegularExpressions;
    if  (!Regex.IsMatch(txtEmail.Text, @"^[a-z,A-Z]{1,10}((-|.)\w+)*@\w+.\w{3}$"))
            MessageBox.Show("Not valid email.");
    
    0 讨论(0)
  • 2020-11-22 12:32
    string patternEmail = @"(?<email>\w+@\w+\.[a-z]{0,3})";
    Regex regexEmail = new Regex(patternEmail);
    
    0 讨论(0)
提交回复
热议问题