We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

gdonald on 01/12/07


Tagged

regex email php


Versions (?)


Who likes this?

3 people have marked this snippet as a favorite

basicmagic
vali29
hudge


PHP Valid Email


Published in: PHP 


  1. error_reporting( E_ALL );
  2.  
  3. function is_valid_email( $email )
  4. {
  5. return preg_match( '/[.+a-zA-Z0-9_-]+@[a-zA-Z0-9-]+.[a-zA-Z]+/', $email );
  6. }
  7.  
  8. $emails = array( 'br0k3n',
  9. 'fred_dred@aol.com',
  10. 'fred-dred@aol.com',
  11. 'fred.dred@aol.com',
  12. 'fred+dred@aol.com' );
  13.  
  14. foreach( $emails as $email )
  15. {
  16. echo $email . ' is' . ( is_valid_email( $email ) ? '' : ' not' ) . " valid\n";
  17. }

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: collin on January 12, 2007

See http://www.ilovejackdaniels.com/php/email-address-validation/ for the proper way to do RFC 2822 validation.

Posted By: tylerhall on January 12, 2007

The above code doesn't work. It lets through the following invalid email addresses:

  • .tyler@foo.com (email cannot start with a period)
  • tyler@foocom (no period between domain and tld)
  • tyler@foo.foooooo (invalid tld)

Also, your regex is limiting the use of many other ASCII characters that RFC-2822 allows in an email address.

You need to login to post a comment.