简体   繁体   中英

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. 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

<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

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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