Javascript - 如果条件由“&&”分隔(需要建议)

时间:2013-08-16 18:43:15

标签: php javascript forms if-statement operators

这应该是一个非常简单的修复...

我有一个注册表单,我希望对空字段进行验证,并在必要时让javascript返回一个简单的弹出警报。这是脚本代码:

<script type="text/javascript">
    function validateForm() {
        var a = document.forms["registration_form"]["school"].value;
        var b = document.forms["registration_form"]["name"].value;
        var c = document.forms["registration_form"]["password"].value;
        var d = document.forms["registration_form"]["field"].value;
        var e = document.forms["registration_form"]["field_location"].value;
        var f = document.forms["registration_form"]["volleyball_campus"].value;
        var g = document.forms["registration_form"]["volleyball_location"].value;
        var h = document.forms["registration_form"]["indoor"].value;
        var i = document.forms["registration_form"]["outdoor_courts"].value;
        var j = document.forms["registration_form"]["blackout"].value;
        var k = document.forms["registration_form"]["blackout_dates"].value;
        if (a == "" || b == "") {
            alert("Please make sure to enter your name and school.");
            return false;
        } else if (c != "pal") {
            alert("Sorry, your password is invalid.");
            return false;
        } else if (d == "no" && e == "") {
            alert("Please make sure to enter where you will play football.");
            return false;
        } else if (f == "no" && g == "") {
            alert("Please make sure to enter where you will play volleyball.");
            return false;
        } else if (h == "no" && i == "0") {
            alert("Please make sure to select how many outdoor volleyball courts you will use.");
            return false;
        } else if (j == "yes" && k == "") {
            alert("Please make sure to enter your blackout dates.");
            return false;
        }
    }
</script>

一切都适用于变量a,b和c。然而,其他条件,从变量d&amp;&amp; e,似乎没有执行。我认为我必须对措辞做错事,但我无法弄清楚它是什么。

我已经设置了我的服务器端php脚本来做同样的事情并且它没有任何问题。你可以在这里看到它:

//Validate the form:
if ($_POST['school'] == "" || $_POST['name'] == "") {
    echo "Please make sure to enter your name and school.";
} elseif ($_POST['password'] != "pal") {
    echo "Sorry, your password is invalid.";
} elseif ($_POST['field'] == "no" && $_POST['field_location'] == "") {
    echo "Please make sure to enter where you will play football.";
} elseif ($_POST['volleyball_campus'] == "no" && $_POST['volleyball_location'] == "") {
    echo "Please make sure to enter where you will play volleyball.";
} elseif ($_POST['indoor'] == "no" && $_POST['outdoor_courts'] == "0") {
    echo "Please make sure to select how many outdoor volleyball courts you will use.";
} elseif ($_POST['blackout'] == "yes" && $_POST['blackout_dates'] == "") {
    echo "Please make sure to enter your blackout dates.";
} else {

这可以按照我的预期方式捕获所有内容,但客户端Javascript验证(我首先运行的只是用户友好的外观)似乎不能超过前三个变量。

我不能为我的生活找出原因。我在互联网上搜索过,但找不到我想要的答案。任何帮助将不胜感激!

P.S。页面本身位于http://www.712jefferson.org/pal/registration.html,如果这有助于澄清预期的过程。

3 个答案:

答案 0 :(得分:2)

仅当d等于no并且e为空时,警报才会显示。这同样适用于fg。这没有意义。举个例子:

我玩volleyball但不要在校园里玩,所以我在no输入f,你的逻辑要求我在g中放一个空字符串。希望用户进入排球位置。

这应该改为:

else if (f == "no" && g != "") {

}  

请注意从==!=的更改,这意味着g不等于空字符串。这同样适用于de(如果e设置为“否”,则您不希望d为空。

同样验证您的密码客户端是有风险的,我希望您有服务器端验证。

答案 1 :(得分:0)

如果后续条件未进行评估,可能是因为已经评估并满足其中一个先前条件,并返回。如果要确认正在评估所有条件,可以删除return语句并向每个输出条件的true / false状态的条件添加console.log()消息,

答案 2 :(得分:0)

试试这个:

else if (registration_form.field[1].checked == true && e == "") {
  alert("Please make sure to enter where you will play football.");
  return false;
  }

并将该单选按钮设为任何一个活动

<input type="radio" name="field" checked="checked" value="yes">
<input type="radio" name="field" value="no">