Ask a questionAsk a question
 

AnswerEmail address regular expression

  • Tuesday, September 15, 2009 2:13 PMwaterding Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I used the function below to validate email addresses.

    // Function to test for valid email address.
            public static bool IsValidEmailAddress(String emailAddress)
            {
                Regex regex = new Regex(@"^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$");
                Match match = regex.Match(emailAddress);
                return match.Success;
            }

    This regular expression works for amy.charles@test.com, but not robert.o'neil@test.com.

    Can anyone help me to modify the regulation expression, making it to valid for robert.o'neil@test.com

    Thanks in advance!

Answers

All Replies

  • Tuesday, September 15, 2009 2:42 PMJohnGrove Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Read this as it discusses that very subject under the heading "Trade-Offs in Validating Email Addresses"
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
  • Tuesday, September 15, 2009 3:04 PMOmegaManMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Email address validation is (IMHO) a worthless task! Check out the RFC standard. I suggest that you make sure you have [^@]+@.* that is the best one. Check out the forum post on why most regex's fail:

    How to: Verify That Strings Are in Valid E-Mail Format

    Phil Haacks comments:

    I Knew How To Validate An Email Address Until I Read The RFC

    and finally the RFC on wikipedia if you are still brave:

    E-mail address

    :-)

    William Wegerson (www.OmegaCoder.Com)
  • Wednesday, September 16, 2009 1:53 PMRohini Chavakula Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Below link will provide complete reference to solve your issue:

    http://www.codeproject.com/KB/validation/Valid_Email_Addresses.aspx




  • Wednesday, September 16, 2009 2:10 PMJohnGrove Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Rohini,
    I just tried the pattern you said was a "complete reference to solve" the issue.

    string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
    Regex re = new Regex(strRegex);
    if (re.Match("robert.o'neil@test.com").Success)
       Console.WriteLine("Success");
    And it didn't match. I would listen to William's advice. He is an expert in Regex.
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
  • Wednesday, September 16, 2009 2:13 PMOmegaManMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    And it didn't match. I would listen to William's advice. He is an expert in Regex.

    I do ok...but the thought is about the email pattern is that, if a user double validates the email address why question what they believe is the real one? Unless you are creating a new emaill address for a email box...what is the real point of true validation.... By simply checking for  characters before a @ sign and then a .ABC it should be a valid one.


    William Wegerson (www.OmegaCoder.Com)
  • Friday, November 06, 2009 3:40 PMEfran Cobisi [cobisi.com] Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi all,

    in my company we use EmailVerify.NET (http://www.emailverify.net), a Microsoft .NET component which supports e-mail syntax checking, domain MX test and mailbox validation too.
    E-mail validation up to the mailbox level is easy as writing the following code block:

    var verifier = new EmailVerifier();
    var result = verifier.Verify("me@example.com", VerificationLevel.Mailbox);
    
    if (result.IsSuccess)
    {
       // Go on, send the message
    }
    

    Additional code samples can be found here.
    From their home page:

    EmailVerify.NET is a powerful .NET component which verifies email addresses using various techniques, including:
    • Syntax verification, according to RFC 2821 and RFC 2822
    • MX record lookup
    • Disposable email address (DEA) validation
    • SMTP availability check
    • Mailbox existence check, with greylisting support
    • Catch-all test

    It is completely written in managed code (C#) and is compliant with the Common Language Specification (CLS), so it can be used with any other .NET language, including VB.NET, C++/CLI, J#, IronPython, IronRuby and F#.

    The price for this component is very small (less than 50 bucks) and their support is one of the greatest I've ever found for a small software company like they are.
    Hope this helps.