How to validate an email address format

There are several ways of validating an email address format.

Normally, I do it the following way:

1. I first define the regular expression for the email format:

define (“FORMAT”,”^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$”);

2. To validate the email address provided is correct:

if ( !eregi (FORMAT, $email)) echo “The email provided has an invalid format”;

Regular expressions can be a pain to understand and if it’s hard for you then I would recommend you read “Sams Teach Yourself Regular Expressions in 10 Minutes” from Ben Forta.

If however you are simply interested in some practical examples that you could use then I recommend “Regular Expressions Cookbook” from Jan Goyvaerts and Steven Levithan.

Be the first to comment

Leave a Reply

Your email address will not be published.


This site uses Akismet to reduce spam. Learn how your comment data is processed.