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 his_emailgmail.com 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 :
<?php
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 qsddsfqsdsdfqsdsqdsqd@gmail.com is not valide, but knows that qsqsdsq@dfsfsqdqsdqd.com 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 :)

35 comments:

Anonymous said...

Alternately:

filter_input(INPUT_POST, 'nameofemailvariablethatyouwantchecked', FILTER_VALIDATE_EMAIL)

http://www.w3schools.com/php/php_ref_filter.asp

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

Thanks!

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
http://html5planet.blogspot.in/2012/07/validate-e-mail-address-with-php.html

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
thnx!!!

Clone PHP Scripts
http://www.scripthood.com

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.

http://www.wmappz.com/php/validate-email-address/

D3 Gold said...

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

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

Unknown said...

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

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

James jones 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!

James jones said...
This comment has been removed by the author.
rohini 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

Weassistplus said...

My gratitude for the wonderful Content. We want you to continue this for us.
Graphic Designing in Coimbatore Visit https://weassistplus.in/
A Super Specialized place for Web Designing, Logo Designing, Brochure, Stationary items, google ads, Social Media Analyst, SEO maintenances for your company.

Araseefoods said...

I am Appreciating & Thanking you for a wonderful article. Thanks a lot.
for good tasty and healthy food products in Erode and Coimbatore visit https://araseefoods.com/
Arasee’s products are
Baji mix flour, Corn Flour,Flattened Rice,Gram Flour,Brown Sugar (Karumbu sakkarai),Maida Flour,Pottu kadalai Flour,Ragi Flour, Rava,Semiya,Wheat Rava,Atta.

alpharealtors said...

We overwhelm this article. This is a big information for us.
Real estates in Coimbatore Alpharealtors (http://galaxyvalley.in/).

Lingaatm said...
This comment has been removed by the author.
weassistplus said...
This comment has been removed by the author.
aswambusinesssolutions said...

This was a great one..
For Web Designing an Graphic Designing in Coimbatore.
Visit https://aswam.in/
Profession Company in Web Designing,
Logo Designing,
Brochure,
A Complete hosting for your concern,
Stationary items for ur organizations.

Anonymous said...

avail of the leading developments with the mobile apps with integration of blockchain application development along with IoT Development Solutions

Veenet Digital said...

Nice Post!!
Please look here at Static Website Design Company in Tirupur

davv ba 2nd year result 2021 name wise said...

Really a very helpful article thanks for sharing and keep on sharing!

Vibin said...
This comment has been removed by the author.