filter_var总是给出电子邮件无效

时间:2019-05-18 18:08:14

标签: php

我将<input type="text" name="url">更改为<input type="email" name="email">,现在无论我说什么,它仅显示EMAIL无效,为什么?

如果我将其更改为=>输入type =“ text” name =“ url,则可以。但是,如果我将其更改为type = email,则它只能以一种方式显示,并且表明Email不仅有效

<!DOCTYPE html>

<html>
<head>
<title> PHP Basics </title>
</head>
<body>

<form action="filters.php" method="post">
    <input type="email" name="email">
    <input type="submit" name="submit" value="Submit Now">
</form>

<?php

if (isset($_POST['submit'])) {
    $email = $_POST['email'];
    if (filter_var($email, FILTER_VALIDATE_URL)) {
        echo "<h2>Email is Valid</h2>";
    } else {
        echo "<h2>Email is NOT Valid</h2>";
    }
}
?>

</body>
</html>

应该显示电子邮件是否有效。没有错误消息弹出。

1 个答案:

答案 0 :(得分:0)

问题出在 FILTER_VALIDATE_URL

您需要验证电子邮件而不是 URL ,因此请使用 FILTER_VALIDATE_EMAIL

这是代码

<!DOCTYPE html>
<html>
<head>
    <title> PHP Basics </title>
</head>
<body>

<form action="filter.php" method="post">
    <input type="email" name="email">
    <input type="submit" name="submit" value="Submit Now">
</form>

<?php
    if (isset($_POST['submit']) && isset($_POST['email'])) {
        $email = $_POST['email'];

        if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
            echo "<h2>Email is Valid</h2>";
        }
        else {
            echo "<h2>Email is NOT Valid</h2>";
        }
    }
?>