We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5568
    • 35 Posts
    From this month’s issue of LinuxWorld Magazine, article at:

    http://www.linuxjournal.com/article/9585

    Validate an E-Mail Address with PHP, the Right Way.



    New Snippet Coding:

    /**
    SOURCE: http://www.linuxjournal.com/article/9585
    
    Validate an email address.
    Provide email address (raw input)
    Returns true if the email address has the email
    address format and the domain exists.
    */
    
    function validEmail($email) {
      $isValid = true;
      $atIndex = strrpos($email, "@");
      if (is_bool($atIndex) && !$atIndex) {
        $isValid = false;
      } else {
        $domain = substr($email, $atIndex+1);
        $local = substr($email, 0, $atIndex);
        $localLen = strlen($local);
        $domainLen = strlen($domain);
        if ($localLen < 1 || $localLen > 64) {
          // local part length exceeded
          $isValid = false;
        } else if ($domainLen < 1 || $domainLen > 255) {
          // domain part length exceeded
          $isValid = false;
        } else if ($local[0] == '.' || $local[$localLen-1] == '.') {
          // local part starts or ends with '.'
          $isValid = false;
        } else if (preg_match('/\\.\\./', $local)) {
          // local part has two consecutive dots
          $isValid = false;
        } else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
          // character not valid in domain part
          $isValid = false;
        } else if (preg_match('/\\.\\./', $domain)) {
          //domain part has two consecutive dots
          $isValid = false;
        } else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) {
          // character not valid in local part unless
          // local part is quoted
          if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","", $local))) {
            $isValid = false;
          }
        }
        if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) {
          // domain not found in DNS
          $isValid = false;
        }
      }
      return $isValid;
    }
    


    All Credits should goto Douglas Lovell, author of the above article.
      • 27376
      • 576 Posts
      Very nice! But why not use a single [tt]preg_match[/tt]?

      <?php
      $valid = preg_match('#<([a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6})))>#i', $email, $matches);
      ?>

      Or, I modified this one to restrict length.
      <?php
      $valid = preg_match('#<([a-z0-9.!\#$%&\'*+-/=?^_`{|}~]{1,64}@([0-9.]+|([^\s]+\.+[a-z]{2,6})))>#i', $email, $matches);
      ?>
        • 5568
        • 35 Posts
        Quote from: sirlancelot at May 17, 2007, 03:30 PM

        Very nice! But why not use a single [tt]preg_match[/tt]?

        <?php
        $valid = preg_match('#<([a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6})))>#i', $email, $matches);
        ?>

        Or, I modified this one to restrict length.
        <?php
        $valid = preg_match('#<([a-z0-9.!\#$%&\'*+-/=?^_`{|}~]{1,64}@([0-9.]+|([^\s]+\.+[a-z]{2,6})))>#i', $email, $matches);
        ?>


        From what I understand of the article you cannot do all possible error checking in one step.  As far as I can see if you can include all the above checks into one preg_match then all is fine, however I do think you might be missing some error checks in your one line code?


        In his article he goes on to explain the reasoning of his script.    His error checking of an email address is for all allowable characters, lengths of the domain and local sections, and possible quoting of characters in an email address. Most email validator scripts do not validate all possible email characters that are in fact allowed to be used. We are limiting our legal email address options when we do this. You should read the above article, I am not sure what I can post online without getting into trouble here.  The coding is not marked with any license, but he encourages us to use it?
          • 25663 MODX Staff
          • 12,272 Posts
          His isn’t a true 100% proper solution. The following would fail (I think) in his code, but is valid per the RFCs:

          "ryan@home"@myplaces.com
            Ryan Thrash, MODX Co-Founder
            Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
            • 27376
            • 576 Posts
            Guess I should shut my mouth since I couldn’t read the article and his reasoning. I can see now though that invalidly quoted emails might validate with a regex match (like having 3 quotes).

            Thanks for the enlightenment smiley