Email address validation in PHP

Advertisement

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: 5% [?]

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

Related Posts

» W3c markup validation and Big websites – Is it really needed?
» Height of various stuffs in IT peoples…
» Php function to validate two decimal places of a number
» Very Cool CV…

16 Comments on “Email address validation in PHP”

  • Anonamoose wrote on 17 February, 2008, 20:19

    Quick heads up, eregi is deprecated use preg_match instead.

  • Vitor Costa wrote on 6 March, 2008, 16:00

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

  • Roshan wrote on 6 March, 2008, 16:42

    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

  • Mukesh wrote on 13 March, 2008, 9:22

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

  • Roshan wrote on 13 March, 2008, 15:50

    Thanks mukesh….

  • baron wrote on 27 May, 2008, 15:16

    thanks

  • andreas beder wrote on 28 September, 2008, 9:26

    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

  • Roshan wrote on 28 September, 2008, 10:19

    @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

  • SilverBulletUK wrote on 28 September, 2008, 16:29

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

  • Roshan wrote on 28 September, 2008, 16:34

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

  • Srinivas wrote on 15 January, 2009, 11:33

    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

  • YVV wrote on 12 March, 2009, 10:04

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

  • YVV wrote on 12 March, 2009, 10:07

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

  • Rajesh wrote on 2 October, 2009, 21:27

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

  • offshore php developer wrote on 23 June, 2010, 10:05

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

    Thanks a lot..!

Trackbacks

  1. PHP Coding School » Blog Archive » php tips [2008-02-17 22:02:07]

Write a Comment

 


Copyright © 2012 Roshan Bhattarai's Blog. All rights reserved.
Powered by WordPress.org, Custom Theme and ComFi.com Calling Card Company.