Friday, February 3, 2017

Server side email validation in php with filter_var

Validating email address on server side is more secure way to prevent from span entry, Some people disabled javascript on their browser and enter wrong or garbage username/email address, So validating email on server side is good approach.

You can use the filter_var() function, which gives you a lot of handy validation and more options.
Note: PHP 5.2.0 or higher is required for the filter_var function. Here is the quick function in php to validate email address in single line.
function checkValidEmail($email){ 
    return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}
 
Above function will validate email like “chirag@gmail” as a valid email, In Standard HTML5 email validation also pass this string too as a valid email, You need to add some extra security in your function to validate TLS so that you can get more filtered email data.
Email with TLS validation.

function checkValidEmail($email){ 
   return filter_var($email, FILTER_VALIDATE_EMAIL) && preg_match('/@.+\./', $email);
}
 

Example:

echo checkValidEmail('chirag@gmail'); // false
echo checkValidEmail('chirag@gmail.'); // false  
echo checkValidEmail('chirag@gmail.com'); // true  
echo checkValidEmail('chirag@gmail.com.'); // false
 

 

2 comments:

  1. Few days ago my friend asked me about email validation service that's why i was looking a website where i can get information about email validation service. Just now i have read this blog content attentively then i got lots of information about email validation service. I am really surprised for that. Thank you so much for your great post.

    ReplyDelete
  2. Email Validation and Verification, Email Checker and Bulk Verify Tool. Using DeBounce remove invalid, disposable, spam-trap, syntax and deactivated emails. email list cleaning service

    ReplyDelete