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 :)

45 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

chinatour said...

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

Unknown 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
https://tokojaring.wordpress.com/
https://jualjaringfutsal.wordpress.com/
https://pasangjaringfutsal.wordpress.com/
http://pancasamudera.wordpress.com/
http://jaringfutsal.wordpress.com/

Unknown 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

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!

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.


https://imediasolutions.in/india/logo-designer-online.php

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.

weassistplus said...

Wonderful post.
https://weassistplus.in/

Web designers in Coimbatore
Graphic designers in Coimbatore
Digital Marketing Company in Coimbatore
SEO Company in Coimbatore
Business promotion Company in Coimbatore
Brochure Designing Company in Coimbatore
Consultant for Organizational improvements in Coimbatore
Best and Quality Web designers in Coimbatore
Fastest Web designers in Coimbatore
Best and Quality Graphic designers in Coimbatore
Fastest Graphic designers in Coimbatore

Lingaatm said...

It’s good to knew these useful post from you. Thank you.
https://lingaatm.com/

Agricultural Machines Manufacturers in Coimbatore
Best Price Machines Manufacturers in Coimbatore
High Quality Agricultutal Machines Manufacturers in Coimbatore
Dehusking Machine Manufacturers in Coimbatore
Coir Fibre Extracting Machine Manufacturers in Coimbatore
Grow Bag Machine Manufacturers in Coimbatore
Pith Blocking Machine Manufacturers in Coimbatore
Fibre Baling Machine Manufacturers in Coimbatore
Shredding Machine Manufacturers in Coimbatore
Pre cutter for Coir Industry & Tea industry Manufacturers in Coimbatore
Garden Article Making Machine Manufacturers in Coimbatore
Husk Chips Cutting Machine Manufacturers in Coimbatore
Tender Coconut Trimming Machine Manufacturers in Coimbatore
Pruning Machine Manufacturers in Coimbatore
Agricultural Sprayer Manufacturers in Coimbatore
Arecanut processing machine Manufacturers in Coimbatore
Briquettes machine Manufacturers in Coimbatore
Muller machine Manufacturers in Coimbatore
Oil press machine Manufacturers in Coimbatore

Araseefoods said...

I am Appreciating & Thanking you for a wonderful article. Thanks a lot.
https://araseefoods.com/

Good tasty and healthy food products in Erode and Coimbatore
Food product manufacturers in Erode and Coimbatore
Food product Suppliers in Erode and Coimbatore
Cheap and Best Food products in Erode and Coimbatore
Bajji mix flour product Manufacturers in Erode and Coimbatore
Corn flour product Manufacturers in Erode and Coimbatore
Flattened Rice product Manufacturers in Erode and Coimbatore
Gram Flour product Manufacturers in Erode and Coimbatore
Karumbu sakkarai product Manufacturers in Erode and Coimbatore
Maida Flour product Manufacturers in Erode and Coimbatore
Pottu kadalai Flour product Manufacturers in Erode and Coimbatore
Ragi Flour product Manufacturers in Erode and Coimbatore
Rava Manufacturers in Erode and Coimbatore
Semiya Manufacturers in Erode and Coimbatore
Atta Manufacturers in Erode and Coimbatore
Wheat Rava Manufacturers in Erode and Coimbatore

galaxyvalley said...

Thank you for the valuable post.
http://galaxyvalley.in/

Beautiful Farm House in Kotagiri
Best Farm House for weekend stay in Kotagiri
Best Holiday stay in Kotagiri
Best Architectural farmhouse in Kotagiri
Farm house near waterfalls and stream in Kotagiri

galaxyvalley said...

We overwhelm this article. This is a big information for us.
https://alpharealtors.in/

Leading Real Estate Company in Coimbatore
Affordable and Luxury Property Sellers in Coimbatore
Value added property Sellers in Coimbatore
Best Residential Property Sellers in Coimbatore
Best Industrial Property Sellers in Company in Coimbatore

weassistplus said...

This was a great one.
https://aswam.in/

Web designers in Coimbatore
Graphic designers in Coimbatore
Digital Marketing Company in Coimbatore
SEO Company in Coimbatore
Business promotion Company in Coimbatore

Designpluz said...

I am very much pleased with the contents you have mentioned. I wanted to thank you for this great article.. Website Design Company Coimbatore. Visit us https://www.designpluz.com

Deepa said...

Never too late to start learning at Salesforce Training in Australia even though you don't have any programming knowledge you can excell in Salesforce Training in London United Kingdom (UK) because it is all about your customers, so this time find the best Salesforce Training in Europe. This way we will learn Salesforce CRM.