Skip to content


How to validate an e-mail address

This is my first tutorial for PHP :)
I wrote it because at almost any project this is a “must have” function.
The function is very very simple, it receive as a parameter an e-mail address and it returns the result of the validation (true if the address is valid or false if it isn’t, simple enough ;) ?).
So … how is this done? … using the EREGI PHP function (look at the end of the tutorial for links) which is a regular expression matcher. After about 35 minutes (I didn’t knew anything about the Regular Expression Syntax) I managed to get a working code for this.
(If you want to copy it use the (Show Plain Code link))

  1. function CheckEmail($email)
  2. {
  3. if ( !eregi( "^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$" , $email))
  4. return false;
  5. else
  6. return true;
  7. }

How to use it? Just like a normal PHP function ….

  1. $e = "test@test.com";
  2. if (CheckEmail($e))
  3. print "Valid E-Mail Address";
  4. else
  5. print "Invalid E-Mail Address";

I hope this will help someone, until the next tutorial .. take care :D

Bibliography :

Posted in PHP Tips & Tricks.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.


Get Adobe Flash playerPlugin by wpburn.com wordpress themes