php电子邮件验证与正则表达式无法正常工作

时间:2014-02-05 07:12:15

标签: email-validation

    Hello friends,

    I have a problem regarding email validation.

    Here is my code:-
    <?php
    $email='abc@test.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.ab.cd.ef.gh.ij.kl';

    $normal = "^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$"; 
    $validButRare = "^[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,})$"; 

    if (eregi($normal, $email)) {
      echo("The address $email is valid and looks normal.");
    }

    else if (eregi($validButRare, $email)) {
      echo("The address $email looks a bit strange but it is syntactically valid. You might want to check it for typos.");
    }

    else {
      echo("The address $email is not valid.");
    }           
    ?>

    problem is this that it is showing above mail id as valid email id.
    and the output is:
    "The address abc@test.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.ab.cd.ef.gh.ij.kl is valid and looks normal."

2 个答案:

答案 0 :(得分:2)

检查域名是否存在的更好方法 如果存在域名,则电子邮件有效,否则电子邮件无效。

<?php
if(domain_exists('test@hotc.nu.in')) {
    echo('This email can be accepted as valid.');
}
else {
    echo('Domain name doesnot exists;  Invalid email.');
}

function domain_exists($email, $record = 'MX'){
    list($user, $domain) = explode('@', $email);
    return checkdnsrr($domain, $record);
}
?>

答案 1 :(得分:1)

试试这个:

$email = 'abc@test.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.co.in.ab.cd.ef.gh.ij.kl';
var_dump(filter_var($email, FILTER_VALIDATE_EMAIL) !== false);