I have n number of choices in a select box. After the user selects one or more choices in the select box and submits the form, I need to check that the user hasn't selected more than four choices.
I tried the following:
function howMany() {
var selObj = document.getElementsByName('xid[]');
var totalChecked = 0;
for (i = 0; i < selObj.options.length; i++) {
if (selObj.options[i].selected) {
totalChecked++;
}
}
if (totalChecked > 4) {
alert("You can't check more than 4 options");
return false;
}
return true;
}