How do I restrict special characters except underscore, hyphen and dot in email field in PHP? -
this question has answer here:
- how validate email address in php 8 answers
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"; }
Comments
Post a Comment