How do I restrict special characters except underscore, hyphen and dot in email field in PHP? -


this question has answer here:

this code.

function emailfield($email) { return filter_var($email, filter_validate_email);    } 

and

if(emailfield($_post['email'])=='')   {     echo "invalid email";   }   else   {   echo "valid";   } 

this allows special characters. want allow underscore, hyphen , dot only. how avoid?

use regex this:

/^[\w\.]+@/ 

then should check if domain exists.

php:

$regex = "/^[\w\.]+@/"; $email = emailfield($_post['email']);  $domain = preg_replace($regex, '', $email); //get domain of email address removing local-part  //using checkdnsrr checking if domain registred if($email == '' && preg_match($regex, $email) && checkdnsrr($domain)){   echo "valid"; } 

see also: http://php.net/manual/en/function.checkdnsrr.php


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -