Email address validation in PHP

Posted on February 17, 2008 
Filed Under php, tips and technique, tutorial

Well you’ve been asking why this person has posted the same stuff which you can find easily in google. Ya you are right you can get lots of scripts but many of them are not useful for me so far. They just validate the email like “info@yahoo.com” but they didn’t validate the email address like “contact@roshanbh.com.np” or even the address like “info@holmesglen.vic.edu.au“.


So I’m posting a email add validation function in PHP which validate those kind of address as well.

Function to validate email address in PHP

function validateEmail($email)
{
   if(eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]{2,3})?$', $email))
      return true;
   else
      return false;
}

Let’s see the explanation of the following regular expression to validate email address in php


^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]{2,3})?$

The “^” sign represents the start of the string.And, [a-zA-Z0-9._-]+ represents the first part of string before “@” sign can consists alpha bates, digits and “.,-” and “_” signs. After that “@” refers that this sign must exist. The next part is name of the domain and “[a-zA-Z0-9-]+” allow alpha bates, digits and “-” sign. After that period(.) should exist and validated by “\.“. And, the next string is TLD or ccTLd so can contain only two to four alphabates and validated by “[a-zA-Z]{2,4}“. The next part of expression “(\.[a-zA-Z]{2,3})?” refers that there will be another two or three alphabates after period(.) but this part is optional which is represented by “?” sign. And the last part is same as previous part and is optional as well.

Benefits:

Thus, this function validate the domain name that ends with .com or com.np or .vic.com.au . Furthermore, this function doesn’t accept the string like “info@homs.edu.” but I’ve some function which accepts the email which ends with period(.).

Well there is a small flaw in this function as well. The first part of the expression can’t begin with digits and is left up to you to make this function robust. If you can’t please let know.

Popularity: 14% [?]

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:


Follow me on twitter at http://twitter.com/roshanbh.

Related Posts

» W3c markup validation and Big websites - Is it really needed?
» Very Cool CV…
» Anti Spam protection for your organization’s email server
» Php function to validate two decimal places of a number

Comments

11 Responses to “Email address validation in PHP”

  1. Anonamoose on February 17th, 2008 8:19 pm

    Quick heads up, eregi is deprecated use preg_match instead.

  2. PHP Coding School » Blog Archive » php tips [2008-02-17 22:02:07] on February 17th, 2008 10:10 pm

    [...] Email Validation in PHP By Roshan Well you’ve been asking why this person has posted the same stuff which you can find easily in google. Ya you are right you can get lots of scripts but many of them are not useful for me so far. They just validate the email like … Roshan Bhattarai’s Blog - PHP… - http://roshanbh.com.np [...]

  3. Vitor Costa on March 6th, 2008 4:00 pm

    How I can validate the extension mail…like:
    blabla@gmail.com (correct)
    blabla@gmail.zzz (incorrect)

  4. Roshan on March 6th, 2008 4:42 pm

    if you need to check weather the domain exist or not then you’ve to use getmxrr and fsockopen function to do this. You can better look at the following link which does the same for you.
    http://www.spoono.com/php/tutorials/tutorial.php?id=41

  5. Mukesh on March 13th, 2008 9:22 am

    nice stuff with proper explanation. next time, I will use this email validation code. keep it up bro !!

  6. Roshan on March 13th, 2008 3:50 pm

    Thanks mukesh….

  7. baron on May 27th, 2008 3:16 pm

    thanks

  8. andreas beder on September 28th, 2008 9:26 am

    hi,
    pls dont use this incorrect regex email validation scripts..
    a valid email can be very complex thing..
    i.e. !#$%&’*+-/=?^_`.{|}~@example.com is a valid email address.
    i recommend php s filter ext for mail validation.
    code example:
    if(!filter_var($email, FILTER_VALIDATE_EMAIL))
    looks very clean hm ?!

    best regards

    nfo

  9. Roshan on September 28th, 2008 10:19 am

    @andreas beder - first of all this is not a incorrect regx at all. Furthermore, it is not the best email validation script also.Furthermore, filter_var() function is only available after PHP 5.2.0 in PHP

  10. SilverBulletUK on September 28th, 2008 4:29 pm

    Any email addresses using the .museum TLD would not validate using this RegExp.

  11. Roshan on September 28th, 2008 4:34 pm

    @SilverBulletUK- thanks for the heads up, I’ve never heard about that TLD..I’ll update the function accordingly..

Leave a Reply