PHP Validate Email Address: Email Validator

When working with forms and interacting with your visitors, you may want to verify if your visitor entered an email address in the right field, and if it is valide or not.

Most of people who are new to php world, just verify if the text box of the email address is filled or not, without verifying the syntax of the email address...

Something like this they use:
if (isset($_POST['email'])) {
// do something

But when you want to develop a php website in the best way, taking care of all little details, you should think what happens when your user does make a mistake in the email address, writing something like this: his_email@gmailcom, or or even he writes ssdfsff (like many people do), I'm sure you would like to inform him and ask him to correct his email address, you maybe also want to check if the domain name of his email address is valid, you can't accept an email like this: blahblah@dfsjhbhjjqsdqq.fdf .

So here is the php script I think you should use everytime you have a form with 'email address' input :
function validate_email($email) {

   //check for all the non-printable codes in the standard ASCII set,
   //including null bytes and newlines, and exit immediately if any are found.
   if (preg_match("/[\\000-\\037]/",$email)) {
      return false;
   $pattern = "/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD";
   if(!preg_match($pattern, $email)){
      return false;
   // Validate the domain exists with a DNS check
   // if the checks cannot be made (soft fail over to true)
   list($user,$domain) = explode('@',$email);
   if( function_exists('checkdnsrr') ) {
      if( !checkdnsrr($domain,"MX") ) { // Linux: PHP 4.3.0 and higher & Windows: PHP 5.3.0 and higher
         return false;
   else if( function_exists("getmxrr") ) {
      if ( !getmxrr($domain, $mxhosts) ) {
         return false;
   return true;
} // end function validate_email

Now you can use this function with the email address you want to verify as parameter, and it will return you true if it valide and false if it is not.
This nice function will not know if an email exist really or not, it just verifies the syntax and the domain name, for example will not know that is not valide, but knows that is an invalid email address .

I hope this help you a lot.
Please leave comments or link to us if you think this is usefull :)


Marshton said...


filter_input(INPUT_POST, 'nameofemailvariablethatyouwantchecked', FILTER_VALIDATE_EMAIL)

Marshton said...

of course you do need the semicolon at the end of that line of code :X

ILyes said...

Thank you Marshton, the php filters are a great solution for this, and for many other problems like validating urls and so much things :)

I will surely post about filters in details in my next post thanks to you :D

Thank you again.

Web Marketing firm said...

Very useful information.. i am new to php. thanks for sharing...

d-NIZ said...

Thanks for the function it's really useful.but i'm wondering "$pattern = "/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD";" variable what does exactly?can you explain it in advance?

Anonymous said...

Thank you. I've been trying to get this solution done well and you've really made that feasible for a new guy like me.


Anonymous said...

Perfect. Just what I was looking for. I especially like the use of
checkdnsrr and how it checks to make sure the function exists before calling it. Great job.

Super Jay said...

Hey, you just saved me 20 bucks!

thanks! Signed up for your feed!

Offshore Software Development Company said...

Your blog is pretty good which has good information regarding development and designing. Nowadays thousands of web designing and .NET Development Company are providing their services because the demand of PHP, ASP.NET, MySQL and iPhone development is very high.

openplus said...

If you simple and effective Script for validating an email id then check this tutorial at

you will find complete code and easy to use process.

web development services said...

Very important and effective codes you have shared and there is no doubt that these codes are very helpful being a professional in this field.

Fred White said...

very useful post

Clone PHP Scripts

TechToyz said...

Very Nice Article

Anonymous said...

Awesome friend! Was trying hard looking for one similar! Many thanks. Also have u got a post anywhere on php security. Cheers

Pedro Pinheiro said...

Nice !!! Very useful. Thank you very much....

GayBoy said...

The problem with the standard PHP function is that it doesn't check on the validity of the domain.

I've made a small piece of code which extends the standard PHP function with also domain checking.

D3 Gold said...

AWESOME function. And wonderful choice of tools to function... Great!! RS Gold Buy Diablo III items

chinatour said... is a professional China travel agency providing spectrum of China tour packages,like Yangtze River cruises,Tibet tours,Silk Road tours,Shangri-la tours,and top China city tours like Beijing tours,Xi'an tours,Shanghai tours,Guilin tours,Hong Kong tours, etc..

LOL elo boosting said...

Thanks Marshton, the actual php filter systems really are a excellent answer with this, as well as for a lot of additional difficulties such as validating web addresses and thus a lot points: )#)

I'll certainly publish regarding filter systems within particulars during my following publish because of a person.

elo boosting

Buy Fifa 14 Coins

Divya Dheen said...

Thanks for the great information in your blog PHP Training in Chennai

jaring safety said...

The article posted was very informative and useful
thanks for sharing.
jaring futsal , jaring futsal murah , jual jaring futsal , toko jaring futsal , agen jaring futsal ,
jaring golf , jual jaring golf , jaring golf murah , jaring polynet , tangga darurat , jaring truk ,
jaring cargo , jaring outbound , jual rumput futsal murah , jual rumput sintetis murah , tali tambang , cargo net ,
jaring tanaman , jaring kassa , jaring proyek , jaring bangunan , jaring gedung , jaring pengaman proyek ,
jaring pengaman bangunan , jaring pengaman gedung , jaring peneduh , jaring waring , kasa hijau , tangga darurat ,
jaring gawang futsal , jaring peneduh , jaring truk

Webgenius Bangalore said...

The blog was absolutely fantastic! Lot of great information which can be helpful in some or the other way. Keep updating the blog, looking forward for more contents...Great job, keep it up..Website Design Bangalore | Web Developers Bangalore

Designpluz said...

Web Design Sydney: It is a great sharing...I am very much pleased with the contents you have mentioned. I wanted to thank you for this great article. .Logo Design Sydney,Logo Design in Coimbatore,Brochure Design in Coimbatore

Garrick Co Ida said...

Great & Useful Blog. Helped me a lot.
PHP Training in Chennai
PHP Online Training India

Ancy merina said...
This comment has been removed by the author.
awsome logo design said...

Umesh Kamat needs no introduction. The actor has carved a niche in Marathi film industry with his commendable talent.  Email Verification

Nokia Hp said...

You should take part in a contest for one of the best blogs on the web. I will recommend this page! Acheter fichier email

Cezar said...

I utilize an email validation tool: Zerobunce Email Validation
And the registration is free and also the first 100 emails. It has a very good API and also 98% accuracy.
I am very happy with it!

iMedia Solutions said...

I was searching for the good topic to research and read, I got it this article on your blog, Thank you so much.

James jones said...
This comment has been removed by the author.
manisha said...

Thanks, i appreciate your blog!
apple service center chennai | ipod service center in chennai | apple iphone service center in chennai | apple service center chennai