Return to Snippet

Revision: 25960
at April 14, 2010 23:28 by philsown


Updated Code
/**
 * emailValid
 * Email Validation Function
 * I take an email as a string and test it against 2 regular expressions.
 *
 * @author Regexp Author unknown
 * @author Phillip Harrington <[email protected]>
 * @param string $email
 * @return bool $valid
 */
function emailValid ($email)
{
  $valid = false;
  $email = trim($email);

  if(0 < strlen($email))
  {
    if(!preg_match('/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/i', $email))
    {
      if(preg_match('/^.+\@(\[?)[-a-zA-Z0-9\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/i', $email))
      {
        $valid = true;
      }
    }
  }

  return $valid;
}

Revision: 25959
at April 14, 2010 23:27 by philsown


Initial Code
/**
 * emailValid
 * Emai Validation Function
 * I take an email as a string and test it against 2 regular expressions.
 *
 * @author Regexp Author unknown
 * @author Phillip Harrington <[email protected]>
 * @param string $email
 * @return bool $valid
 */
function emailValid ($email)
{
  $valid = false;
  $email = trim($email);

  if(0 < strlen($email))
  {
    if(!preg_match('/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/i', $email))
    {
      if(preg_match('/^.+\@(\[?)[-a-zA-Z0-9\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/i', $email))
      {
        $valid = true;
      }
    }
  }

  return $valid;
}

Initial URL
http://www.phillipharrington.com/

Initial Description
I used to have the strlen block and the 2 preg if blocks each separately return false throughout, but lately I'm in a single point of exit mindset.

I cannot take credit for the regexps - I borrowed them from someone who borrowed them. If the real author is known, please comment and attribute.

I _did_ however make one edit, which was to increase the TLD portion from 3 chars max to 4 - to account for .info email addresses. One character - my big contribution!

Enjoy

Initial Title
Email Validation Function

Initial Tags
email, php, validation

Initial Language
PHP