0

我有一个验证 if 语句,主要检查两件事。首先:查看名为“why”的下拉框是否为空,如果为空则会向前端发送错误消息,说明我们需要您给我们一个合理的理由。这很好用。现在我有第二个条件,如果出于任何原因“为什么”下拉框中的值是“其他”并且如果评论框为空,那么这将引发另一个错误。现在这两个工作正常,如果它是空的会说“请在评论框中解释您访问的性质!”

我的问题是我试图让评论框的长度在 15 到 45 个字符之间。我一直在使用这个 signinpage.php 验证大约 20 几个小时,以使其完全符合我的要求,但我无法到达我想要的地方。任何帮助都会很可爱!

if(empty($why)) {
            $errors[] = 'Please make sure to select the proper reasoning for your vistit today!';
    } 
        elseif ($why ==='Other' && empty($comments)) {
            $errors[] = 'Please explain the nature of your visit in the comments box!';

        if (strlen($comments) < 15) {
            $errors[] = 'Your explaination is short, please revise!';
    }
        if(strlen($comments) > 45) {
            $errors[] = 'Your explaintion is to long, please revise!';
    }
    }

缩进:

if(empty($why)) {
    $errors[] = 'Please make sure to select the proper reasoning for your vistit today!';
} 
elseif ($why ==='Other' && empty($comments)) {
    $errors[] = 'Please explain the nature of your visit in the comments box!';

    if (strlen($comments) < 15) {
        $errors[] = 'Your explaination is short, please revise!';
    }
    if(strlen($comments) > 45) {
        $errors[] = 'Your explaintion is to long, please revise!';
    }
}
4

2 回答 2

0

如果你正确地复制和粘贴了这个,那么你的括号会把事情搞砸。将所有评论验证放入一个部分,仅在$why==="Other".

if(empty($why))
    $errors[] = 'Please make sure to select the proper reasoning for your vistit today!';
elseif ($why ==='Other'){
    if(empty($comments))
        $errors[] = 'Please explain the nature of your visit in the comments box!';
    else{
        if (strlen($comments) < 15)
            $errors[] = 'Your explaination is short, please revise!';
        if(strlen($comments) > 45)
            $errors[] = 'Your explaintion is to long, please revise!';
    }
}
于 2012-12-27T17:04:01.807 回答
0
if(empty($why)) {
        $errors[] = 'Please make sure to select the proper reasoning for your vistit today!';
} 
elseif ($why ==='Other' && empty($comments)) {
        $errors[] = 'Please explain the nature of your visit in the comments box!';
}
elseif($why === 'Other'){
    if (strlen($comments) < 15) {
        $errors[] = 'Your explaination is short, please revise!';
    }
    if(strlen($comments) > 45) {
        $errors[] = 'Your explaintion is to long, please revise!';
    }
}

您将长度检查包含在要求注释为空的检查中。

于 2012-12-27T17:04:57.777 回答