Email address validation in PHP

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.

16 thoughts on “Email address validation in PHP

  1. Quick heads up, eregi is deprecated use preg_match instead.

  2. Vitor Costa

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

  3. 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

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

  5. Thanks mukesh….

  6. thanks

  7. 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

  8. @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

  9. SilverBulletUK

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

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

  11. Srinivas

    Hai,

    how can check whether the email address is exists or not..
    suppose ,
    123@yahoo.com
    here domain is correct but its not a valid address..
    pl help

  12. YVV

    How to validate my form I dont know, please help me
    here is the url
    http://www.gazeboentertainment.com/47/

  13. YVV

    or please help me to use the given code, how can i use this code into my form’s code ?
    pls help

  14. Rajesh

    @Srinivas The best way to check if an email exists or not is to send verification to that email address with some random verification code.
    If the email is valid the user clicks the verification link or produce verification code and the email is verified.

  15. Hi..,
    I say to thanks. its a nice script and easy to capture of idea..
    Doing well..:-)

    Thanks a lot..!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>