Written a function that checks if a user has checked more than three boxes on a form(maximum amount). There are 17 check boxes so there is quite alot of code, I want to put this into an array - can anyone help me with this?? See the code below
function countChoices(obj) {
var max = 3; // Max of checkboxes selected
box1 = document.frmHandsetRepair.reason1.checked;
box2 = document.frmHandsetRepair.reason2.checked;
box3 = document.frmHandsetRepair.reason3.checked;
box4 = document.frmHandsetRepair.reason4.checked;
box5 = document.frmHandsetRepair.reason5.checked;
box6 = document.frmHandsetRepair.reason6.checked;
box7 = document.frmHandsetRepair.reason7.checked;
box8 = document.frmHandsetRepair.reason8.checked;
box9 = document.frmHandsetRepair.reason9.checked;
box10 = document.frmHandsetRepair.reason10.checked;
box11 = document.frmHandsetRepair.reason11.checked;
box12 = document.frmHandsetRepair.reason12.checked;
box13 = document.frmHandsetRepair.reason13.checked;
box14 = document.frmHandsetRepair.reason14.checked;
box15 = document.frmHandsetRepair.reason15.checked;
box16 = document.frmHandsetRepair.reason16.checked;
box17 = document.frmHandsetRepair.reason17.checked;
var count = (box1 ? 1 : 0) + (box2 ? 1 : 0) + (box3 ? 1 : 0)
count+= (box4 ? 1 : 0) + (box5 ? 1 : 0) + (box6 ? 1 : 0)
count+= (box7 ? 1 : 0) + (box8 ? 1 : 0) + (box9 ? 1 : 0)
count+= (box10 ? 1 : 0) + (box11 ? 1 : 0) + (box12 ? 1 : 0)
count+= (box13 ? 1 : 0) + (box14 ? 1 : 0) + (box15 ? 1 : 0)
count+= (box16 ? 1 : 0) + (box17 ? 1 : 0);
// If you have more checkboxes on your form
// add more (box_ ? 1 : 0) 's separated by '+'
if (count > max) {
alert("You can only choose up to " + max + " choices! \nUncheck an option if you want to pick another.");
obj.checked = false;
}
}
function countChoices(obj) {
var max = 3; // Max of checkboxes selected
box1 = document.frmHandsetRepair.reason1.checked;
box2 = document.frmHandsetRepair.reason2.checked;
box3 = document.frmHandsetRepair.reason3.checked;
box4 = document.frmHandsetRepair.reason4.checked;
box5 = document.frmHandsetRepair.reason5.checked;
box6 = document.frmHandsetRepair.reason6.checked;
box7 = document.frmHandsetRepair.reason7.checked;
box8 = document.frmHandsetRepair.reason8.checked;
box9 = document.frmHandsetRepair.reason9.checked;
box10 = document.frmHandsetRepair.reason10.checked;
box11 = document.frmHandsetRepair.reason11.checked;
box12 = document.frmHandsetRepair.reason12.checked;
box13 = document.frmHandsetRepair.reason13.checked;
box14 = document.frmHandsetRepair.reason14.checked;
box15 = document.frmHandsetRepair.reason15.checked;
box16 = document.frmHandsetRepair.reason16.checked;
box17 = document.frmHandsetRepair.reason17.checked;
var count = (box1 ? 1 : 0) + (box2 ? 1 : 0) + (box3 ? 1 : 0)
count+= (box4 ? 1 : 0) + (box5 ? 1 : 0) + (box6 ? 1 : 0)
count+= (box7 ? 1 : 0) + (box8 ? 1 : 0) + (box9 ? 1 : 0)
count+= (box10 ? 1 : 0) + (box11 ? 1 : 0) + (box12 ? 1 : 0)
count+= (box13 ? 1 : 0) + (box14 ? 1 : 0) + (box15 ? 1 : 0)
count+= (box16 ? 1 : 0) + (box17 ? 1 : 0);
// If you have more checkboxes on your form
// add more (box_ ? 1 : 0) 's separated by '+'
if (count > max) {
alert("You can only choose up to " + max + " choices! \nUncheck an option if you want to pick another.");
obj.checked = false;
}
}