Regex Email validation

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

    I've created a FormValidationUtils class to validate email:

    public static class FormValidationUtils
    {
        const string ValidEmailAddressPattern = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$";
    
        public static bool IsEmailValid(string email)
        {
            var regex = new Regex(ValidEmailAddressPattern, RegexOptions.IgnoreCase);
            return regex.IsMatch(email);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 12:36

    STRING SEARCH USING REGEX METHOD IN C#

    How to validate an Email by Regular Expression?

    string EmailPattern = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
    if (Regex.IsMatch(Email, EmailPattern, RegexOptions.IgnoreCase))
    {
        Console.WriteLine("Email: {0} is valid.", Email);
    }
    else
    {
        Console.WriteLine("Email: {0} is not valid.", Email);
    }
    

    Use Reference String.Regex() Method

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

    There's no perfect regular expression, but this one is pretty strong, I think, based on study of RFC5322. And with C# string interpolation, pretty easy to follow, I think, as well.

    const string atext = @"a-zA-Z\d!#\$%&'\*\+-/=\?\^_`\{\|\}~";
    var localPart = $"[{atext}]+(\\.[{atext}]+)*";
    var domain = $"[{atext}]+(\\.[{atext}]+)*";
    Assert.That(() => EmailRegex = new Regex($"^{localPart}@{domain}$", Compiled), 
    Throws.Nothing);
    

    Vetted with NUnit 2.x.

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

    TLD's like .museum aren't matched this way, and there are a few other long TLD's. Also, you can validate email addresses using the MailAddress class as Microsoft explains here in a note:

    Instead of using a regular expression to validate an email address, you can use the System.Net.Mail.MailAddress class. To determine whether an email address is valid, pass the email address to the MailAddress.MailAddress(String) class constructor.

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

    This saves you a lot af headaches because you don't have to write (or try to understand someone else's) regex.

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

    I have an expression for checking email addresses that I use.

    Since none of the above were as short or as accurate as mine, I thought I would post it here.

    @"^[\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*"
    + "@"
    + @"((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3}))$";
    

    For more info go read about it here: C# – Email Regular Expression

    Also, this checks for RFC validity based on email syntax, not for whether the email really exists. The only way to test that an email really exists is to send and email and have the user verify they received the email by clicking a link or entering a token.

    Then there are throw-away domains, such as Mailinator.com, and such. This doesn't do anything to verify whether an email is from a throwaway domain or not.

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

    This does not meet all of the requirements of RFCs 5321 and 5322, but it works with the following definitions.

    @"^([0-9a-zA-Z]([\+\-_\.][0-9a-zA-Z]+)*)+"@(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]*\.)+[a-zA-Z0-9]{2,17})$";
    

    Below is the code

    const String pattern =
       @"^([0-9a-zA-Z]" + //Start with a digit or alphabetical
       @"([\+\-_\.][0-9a-zA-Z]+)*" + // No continuous or ending +-_. chars in email
       @")+" +
       @"@(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]*\.)+[a-zA-Z0-9]{2,17})$";
    
    var validEmails = new[] {
            "ma@hostname.com",
            "ma@hostname.comcom",
            "MA@hostname.coMCom",
            "m.a@hostname.co",
            "m_a1a@hostname.com",
            "ma-a@hostname.com",
            "ma-a@hostname.com.edu",
            "ma-a.aa@hostname.com.edu",
            "ma.h.saraf.onemore@hostname.com.edu",
            "ma12@hostname.com",
            "12@hostname.com",
    };
    var invalidEmails = new[] {
            "Abc.example.com",     // No `@`
            "A@b@c@example.com",   // multiple `@`
            "ma...ma@jjf.co",      // continuous multiple dots in name
            "ma@jjf.c",            // only 1 char in extension
            "ma@jjf..com",         // continuous multiple dots in domain
            "ma@@jjf.com",         // continuous multiple `@`
            "@majjf.com",          // nothing before `@`
            "ma.@jjf.com",         // nothing after `.`
            "ma_@jjf.com",         // nothing after `_`
            "ma_@jjf",             // no domain extension 
            "ma_@jjf.",            // nothing after `_` and .
            "ma@jjf.",             // nothing after `.`
        };
    
    foreach (var str in validEmails)
    {
        Console.WriteLine("{0} - {1} ", str, Regex.IsMatch(str, pattern));
    }
    foreach (var str in invalidEmails)
    {
        Console.WriteLine("{0} - {1} ", str, Regex.IsMatch(str, pattern));
    }
    
    0 讨论(0)
提交回复
热议问题