• support@answerspoint.com

Using a regular expression to validate an email address

431

Over the years I have slowly developed a regular expression that validates MOST email addresses correctly, assuming they don't use an IP address as the server part.

I use it in several PHP programs, and it works most of the time. However, from time to time I get contacted by someone that is having trouble with a site that uses it, and I end up having to make some adjustment (most recently I realized that I wasn't allowing 4-character TLDs).

What's the best regular expression you have or have seen for validating emails?

I've seen several solutions that use functions that use several shorter expressions, but I'd rather have one long complex expression in a simple function instead of several short expression in a more complex function.

  • PHP

  • asked 2 years ago
  • B Butts

2Answer


0

The format for e-mail addresses is specified in a number of RFCs; it's a pet peeve of mine when people "validate" away perfectly valid addresses, for instance: websites that think all domains end in .com, .net, .edu, or .org; and agents that refuse to transfer mail with a + in the local-part. To that end, I wrote my own regular expression that (I believe) follows the specification, which I'll share below.

First, I'd like to share some code that Igor found, which he considers a masterpiece.

 

  if(document.forms[0].c_email.value) {
      var add = document.forms[0].c_email.value;
      var ampisthere = false;
      var spacesthere = false;

      var textbeforeamp = false;
      var textafteramp = false;
      var dotafteramp = false;
      var othererror = false;

      for(var i = 0; i < add.length; ++i) {
          if(add.charAt(i) == '@') {
              if(ampisthere)
                  othererror = true;

              ampisthere = true;
          } else if(!ampisthere)
              textbeforeamp = true;

          else if(add.charAt(i) == '.')
              dotafteramp = true;

          else
              textafteramp = true;

          if(add.charAt(i) == ' ' || add.charAt(i) == ',')
              spacesthere = true;

      }

      if(spacesthere || !ampisthere || !textafteramp
      || !textbeforeamp || !dotafteramp || othererror)
      {
          error += "\tEmail addresses must be valid working";
          error += " addresses with no commas or spaces\n";
      }
  }

 

Obviously, it's JavaScript. Unfortunately, there was no equivalent check being done server-side.

And as I promised, here's my own RegExp for you to tear apart. (Yes, I know it doesn't handle a quoted local-part. No, I don't mind. Seriously, who does that?)

^[-!#$%&'*+/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+/0-9=?A-Z^_a-z{|}~])*
@[a-zA-Z](-?[a-zA-Z0-9])*(\.[a-zA-Z](-?[a-zA-Z0-9])*)+$
 
  • answered 2 years ago
  • Sunny Solu

0

The format for e-mail addresses is specified in a number of RFCs; it's a pet peeve of mine when people "validate" away perfectly valid addresses, for instance: websites that think all domains end in .com, .net, .edu, or .org; and agents that refuse to transfer mail with a + in the local-part. To that end, I wrote my own regular expression that (I believe) follows the specification, which I'll share below.

First, I'd like to share some code that Igor found, which he considers a masterpiece.

 

  if(document.forms[0].c_email.value) {
      var add = document.forms[0].c_email.value;
      var ampisthere = false;
      var spacesthere = false;

      var textbeforeamp = false;
      var textafteramp = false;
      var dotafteramp = false;
      var othererror = false;

      for(var i = 0; i < add.length; ++i) {
          if(add.charAt(i) == '@') {
              if(ampisthere)
                  othererror = true;

              ampisthere = true;
          } else if(!ampisthere)
              textbeforeamp = true;

          else if(add.charAt(i) == '.')
              dotafteramp = true;

          else
              textafteramp = true;

          if(add.charAt(i) == ' ' || add.charAt(i) == ',')
              spacesthere = true;

      }

      if(spacesthere || !ampisthere || !textafteramp
      || !textbeforeamp || !dotafteramp || othererror)
      {
          error += "\tEmail addresses must be valid working";
          error += " addresses with no commas or spaces\n";
      }
  }

 

Obviously, it's JavaScript. Unfortunately, there was no equivalent check being done server-side.

And as I promised, here's my own RegExp for you to tear apart. (Yes, I know it doesn't handle a quoted local-part. No, I don't mind. Seriously, who does that?)

^[-!#$%&'*+/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+/0-9=?A-Z^_a-z{|}~])*
@[a-zA-Z](-?[a-zA-Z0-9])*(\.[a-zA-Z](-?[a-zA-Z0-9])*)+$
  • answered 2 years ago
  • G John

Your Answer

    Facebook Share