简体   繁体   English

验证2个复选框和2个文本框

[英]Validate 2 check boxes and 2 text boxes

My goal is user require to choose at least one option from 2 check boxes and 2 text boxes. 我的目标是用户需要从2个复选框和2个文本框中至少选择一个选项。 If user do not select anything, the system will prompt the user to select at least one. 如果用户未选择任何内容,系统将提示用户选择至少一项。

However, right now, the user need to select everything which is not what I want. 但是,现在,用户需要选择我不想要的所有内容。

Below are my codes.. 以下是我的代码。

Help will be appreciate..Thanks! 帮助将不胜感激..谢谢! :) :)

At index.jsp 在index.jsp

<form method="post" name="searchform" onsubmit="return validateForm();">
Date: <input name="date" readonly="readonly" />
Number: <input type="text" name="on">
<input type="checkbox" id="completed"> Option A
<input type="checkbox" id="pending"> Option B
<input type="submit" value="Submit">
</form>

At javascript JavaScript

function validateForm() {
    var radio1 = document.getElementById('pending').checked;
    var radio2 = document.getElementById('completed').checked;

    var on = document.forms["searchform"]["on"].value;
    var date = document.forms["searchform"]["date"].value;


    if ((radio1 == "") || (radio2 == "") || (on == null || on="") || (date == null || date =="")){
        alert('Please Choose at least 1 Option');
        return false;
    }

}

simply change your outer || 只需更改您的外部|| to && &&

if ((radio1 == "") && (radio2 == "") && (on == null || on="") && (date == null || date =="")){
    alert('Please Choose at least 1 Option');
    return false;
}

Try this one 试试这个

if (((radio1 == "") && (radio2 == "")) &&(on == null || on="") && (date == null || date =="")){
        alert('Please Choose at least 1 Option');
        return false;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM