简体   繁体   中英

Validating multiple drop downs

thisI have four drop downs on my web email form as below

<select  name="newstaff"  class="formStyle" style="width:335px;" >
<option value="N/A">Please Select</option>
---- other options ---
<select name="allstaff"  class="formStyle" style="width:335px;" >
<option value="N/A">Please Select</option>
 ---- other options ---
<select  name="learning"  class="formStyle" style="width:400px;" >
<option value="N/A">Please Select</option>
---- other options ---
<select  name="leaders"  class="formStyle" style="width:335px;" >
<option value="N/A">Please Select</option>
---- other options ---

How would I write a validation function that would bring up an alert if a user tried to submit this form without selecting at least one of the other options from at least one drop down "N/A

thanks

Edit:

Code used to validate form:

function validateForm(objForm)
{
    var returnStatus = 1;

    if (objForm.newstaff.selectedIndex == 0) {
        alert("please make a selection!");
        returnStatus = 0;
    };
    if (returnStatus) {
        objForm.submit();
    }
}

You were close. The difference here getting the array of elements with the tag name of select .

validateForm = function(objForm) {
    var i = 0,
        valid = false,
        elements = objForm.getElementsByTagName("select");

    for ( ; i < elements.length; i++) {
        if (elements[i].selectedIndex) {
            valid = true;
            break;
        }
    }

    if (valid === true) {
        objForm.submit();
    } else {
        alert ("Please make a selection!");
    }
}

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