Posted By

Wardy on 09/24/08


email php validate

Versions (?)

Who likes this?

3 people have marked this snippet as a favorite


php - validating email address - Reg Expresion

 / Published in: PHP


  1. <?php
  2. $string = "[email protected]";
  3. if (preg_match(‘/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2}$/’,
  4. $string))
  5. {
  6. echo "example successful.";
  7. }
  8. ?>

Report this snippet  


RSS Icon Subscribe to comments
Posted By: Scooter on September 28, 2008

Some of the characters in the code snippet seem to have become garbled somehow during the copy-paste, but it's readable on the linked blog article.

Some top-level domains are more than 4 characters long, such as .museum; it would be easy enough to add support for this by removing the number 4 after the comma.

Your regex does not support plus signs in the local part of the e-mail address. For example, [email protected] would fail the test, but it is a valid e-mail address.

There are some other things not supported which are less important, like IP address for the domain part, quoted local parts, etc. If you want to learn more about them you can check out my own e-mail address validation routine here on Snipplr.

You need to login to post a comment.