验证电子邮件错误

时间:2012-01-10 00:44:45

标签: php validation

<?php
if (isset($_POST['ign'], $_POST['email'])) {
    if($_POST['ign'] && $_POST['email']){

    }
    else {
        echo ("Please enter all of the values!");
    }
}
else {
    echo ("Error in form data!");
}
if((FILTER_VALIDATE_EMAIL($_POST['email'] == TRUE))) {
    $email = $_POST['email'];
    echo ("Thanks, " . htmlentities($_POST['ign']) . ", you will recieve an email when the site is complete!");
}
else {
    echo "Failure!";
}

// insert email and ign into database
?>

这是否能正常工作?第一次从头开始做一些事情哈哈!

OK!我改变了它。那这个呢?我也应该做空的吗?

<?php
if (!isset($_POST['ign'], $_POST['email'])) {
    if($_POST['ign'] && $_POST['email']){
    echo "Please fill out all of the fields!";
        die;
}
if(var_filter($_POST['email'], FILTER_VALIDATE_EMAIL))
    $email = $_POST['email'];
    echo ("Thanks, " . htmlentities($_POST['ign']) . ", you will recieve an email when the site is complete!");
}
else {
    echo "Your email was invalid!";
}

// insert email and ign into database
?>

2 个答案:

答案 0 :(得分:0)

使用内置功能,不要重新发明轮子:

if(filter_var($mail, FILTER_VALIDATE_EMAIL)){
  echo "Mail is valid!";
}

为什么你的功能名称是全部大写?

...你看到这个......

之间的区别吗?
if(func($_POST['email'] == TRUE)){

和... ..

if(func($_POST['email']) == TRUE){

答案 1 :(得分:0)

那里有很多错误。这是你应该做的:

// First check if both fields are present. Usually there is no point in doing this
// because the next check will also catch this case, but you had it so I put it in too.
if (!isset($_POST['ign'], $_POST['email'])) { 
    echo ("Error in form data!"); 
    die; // or something else
}

// Then check if both values are non-"empty" (you might want to look at the docs for
// an explanation of what this means exactly).
if (empty($_POST['ign']) || empty($_POST['email'])) { 
    echo ("Please enter all of the values!"); 
    die; // or something else
}

// Finally, validate the email. DO NOT COMPARE WITH true!
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    echo "Failure!"; 
    die; // etc
}

echo ("Thanks, " . htmlentities($_POST['ign']) . ", blah blah!"); 
相关问题