douzi7890 2015-12-13 16:20
浏览 70
已采纳

FILTER_VALIDATE_EMAIL出错

For some reasons, that are unclear for me, I can't use this code on my website: when I use it the php form doesn't load.

<?php
$email = "john.doe@example.com";
$email = (filter_var($email, FILTER_SANITIZE_EMAIL));       
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
    echo("$email is a valid email address");
} else {
    echo("$email is not a valid email address");
}

This one works but it doesn't give an error when there isn't a . after the @ in the email, so the email below get validated

$email = "john.doe@examplecom";
$email = (filter_var($email, FILTER_SANITIZE_EMAIL));

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo("$email is a valid email address");
} else {
    echo("$email is not a valid email address");
}
?>

How can I get my form to validate only email with an @ and a . after the @

Edit:

        $authorEmail = (filter_var($_POST['formEmail'], FILTER_SANITIZE_EMAIL));
        if (!(filter_var($authorEmail, FILTER_VALIDATE_EMAIL))){
            $emailError = true;
            $hasError = true;
        } else{
            $formEmail = $authorEmail;
        };
  • 写回答

1条回答 默认 最新

  • dongxi2014 2015-12-13 16:33
    关注

    You shouldn't try to enforce validation that is too strict. your second example ("john.doe@examplecom") is a valid email address according to the relevant RFC standards (5321, 5322 and 6531). Other valid email addresses include:

    • "()<>[]:,;@\\"!#$%&'*+-/=?^_`{}| ~.a"@example.org
    • user@[IPv6:2001:db8::1]
    • user@com

    This one has to be posted as code because it contains so many weird characters:

    #!$%&'*+-/=?^_`{}|~@example.org
    

    examples taken from email wiki

    The closest you can get to an RFC compliant home-grown validation system is to use an insanely complex regex: like this one

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭