var($email, FILTER

I am validating email using- filter_var($email, FILTER_VALIDATE_EMAIL). I want to echo a statement saying 'invalid email' if the above function returns false. However irrespective of if-statement 'invalid email' echoed on the screen from the beginning of time when page loads. Help.

This is my code:

<?php $emailErr = ""; ?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<ul class="form-style-1">
<li>
<label for="email">Email</label>
<input placeholder="Email*" type="email" name="email" id="email" required>
</li>
<?php 
if(filter_var($email, FILTER_VALIDATE_EMAIL) == false){
$emailErr = "Invalid email.";
echo $emailErr;}?>
</ul>
</form>

it seems that you don't check if $email is set before validating the email. as an empty string or null is not a valid email, the test fails and the message invalid email is displayed

You should use this :

if(!empty($email) && filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
    $emailErr = "Invalid email.";
    echo $emailErr;
}
链接地址: http://www.djcxy.com/p/16544.html

上一篇: 使用jQuery和正则表达式验证电子邮件地址

下一篇: var($ email,FILTER