javascript - JS: looping function through checkbox array not counting properly -
i want validate input on series of checkboxes. there must @ least 1 checkbox selected, otherwise user gets alert select one. however, alert appears unless of checkboxes selected.
i realize issue how loop params set, cannot figure out how fix it.
for(var i=0;i<7;i++){ if( !checkboxes[i].checked ){ alert( 'you need select @ least 1 day!'); checkboxes[i].focus(); return false; } }
you can use flag set validation status , set true if atleast 1 item checked.
then after loop check whether flag set else there no checkboxes selected.
var valid = false; (var = 0; < checkboxes.length; i++) { if (checkboxes[i].checked) { valid = true; break; } } if (!valid) { alert('you need select @ least 1 day!'); checkboxes[0].focus(); return false; }
Comments
Post a Comment