E-mail Address Validation
Regular Expression + PHP
back

E-mail Address Standards
From time to time, people ask me about validating email addresses: not how to tell if an account or domain exists but whether the address itself is valid. There are a lot of bad regular expressions out there that check this. Here's a good one. Please note that there are limits on the length of both the local-part (64 characters) and the domain (255 characters); these limits are not checked by the expression but are checked in the PHP code. [ RFC2822, Wikipedia ]
Warning: This script is provided as is with no warranty implied or otherwise. Users must determine whether it is fit for any particular purpose, as we, the authors, make no claim to that end. Use it at your own risk.
The following expression and code are licensed under the Academic Free License v3.0.

The Regular Expression
^[-!#$%&'*+/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])*)+$

The PHP Function
<?php function isValidAddress( $email, $check = false )
{
  ##############################
  # PHP Email Address Validator
  # (C) Derrick Pallas
  #  
  # Authors: Derrick Pallas
  # Website: http://derrick.pallas.us/email-validator/
  # License: Academic Free License 3.0
  # Version: 2006-12-01a

  if (!ereg(''
      . '^'
      . '[-!#$%&\'*+/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])*)+'
      . '$'
      , $email
      )    ) return false;
  list( $local, $domain ) = split( "@", $email, 2 );
  if ( strlen($local) > 64 || strlen($domain) > 255 ) return false;
  if ( $check && !gethostbynamel( $domain ) ) return false;
  return true;

  # END
  ######
}
?>
          
Valid HTML 4.01 Transitional