仅在输入信息时才进行表单验证

时间:2014-03-21 19:07:53

标签: php forms validation

我对电子邮件地址进行了一些客户端表单验证。基本上它会检查电子邮件是否太短,如果它太长,最后使用FILTER_VALIDATE_EMAIL检查它是否有效。

然而,对表格进行了调整。电子邮件字段不再是必需。但我不只是想删除所有验证,因为我仍然想确保输入电子邮件地址是正确的。它不再是必填字段。

以下是目前验证的嵌套if语句:

if (isset($_POST['email_address'])){
    $email_address = mysql_real_escape_string(trim($_POST['email_address']));
    $_SESSION['status']['register']['email_address'] = $email_address;
    if(strlen($email_address) > 10){ // email address less than 10
        if(strlen($email_address) < 161){ // if longer than 160
            if(email_valid($email_address) == false){ // email address invalid format
                $_SESSION['status']['register']['error'][] = "The email address has been put in wrong. Please check and try again.";
            }else{ 
                // passed min length, passed max length, passed validation
            }
        }else {
            $_SESSION['status']['register']['error'][] = 'The email address is too long.';
        }
    } else{
        $_SESSION['status']['register']['error'][] = "The email address is too short. It can't be shorter than 10 letters.";
    }
}else{
    $_SESSION['status']['register']['error'][] = "You haven't put in an email address.";
}

但是我需要改变它来做以下事情,但是我无法理解嵌套语句:

  1. 检查是否输入了任何内容。 2.如果是,请按照上述步骤进行操作。
  2. 如果不是,无论如何都要进步。
  3. 因此,只有当他们实际输入任何内容时,验证才会运行。此外,继续进行表格检查仍然可以,因为无论如何都不需要。完整的代码可以在这里看到pastebin.com

4 个答案:

答案 0 :(得分:1)

将isset更改为!empty,并删除之前建议的其他内容。

if(!empty($_POST['email_address']) {
//do stuff
}

答案 1 :(得分:0)

如果您删除了最后的else,那么您将会很高兴。

答案 2 :(得分:0)

我相信,如果我不误,应该怎么做。

if (!empty($_POST['email_address'])) {
    $email_address = mysql_real_escape_string(trim($_POST['email_address']));
    $_SESSION['status']['register']['email_address'] = $email_address;
    if(strlen($email_address) > 10){ // email address less than 10
        if(strlen($email_address) < 161){ // if longer than 160
            if(email_valid($email_address) == false){ // email address invalid format
                $_SESSION['status']['register']['error'][] = "The email address has been put in wrong. Please check and try again.";
            } else {
                // passed min length, passed max length, passed validation
            }
        } else { 
            $_SESSION['status']['register']['error'][] = 'The email address is too long.';
        }
    } else {
        $_SESSION['status']['register']['error'][] = "The email address is too short. It can't be shorter than 10 letters.";
    }
}

答案 3 :(得分:0)

也许你可以试试这个,使用起来更友好,更容易验证: 插入数据库时​​请使用PDO或mySQLi

<?php
function output_errors($error) {
    echo '<ul>';
    foreach($error as $err) {
        echo '<li>'.$err.'</li>';
    }
    echo '</ul>';
}

$email = mysql_real_escape_string(strip_tags($_POST['email_address']));
if(!empty($email)) {
    if(strlen($email) > 10) {
        $error[] = 'The email address is too short. It can\'t be shorter than 10 letters.';
    }

    if(strlen($email) < 161) {
     $error[] = 'The email address is too long.';
    }

    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error[] = 'The email address has been put in wrong. Please check and try again.';
    }

    if(!empty($error)) {
        echo output_errors($error);
    } else {
        //do something
    }
}
?>