Forum Moderators: open
How do I validate that at least one checkbox is checked?
I tried to use radio boxes validation:
var noChoice = true;
for(i=0;i < document.music.length;i++)
{
if (document.music[i].checked)
noChoice = false;
}
if(noChoice)
{
alert("...error...!");
return false;
}
but it didn`t work!
Thank you!
I have to use that validation in between many other validations for other fields such as...
<script language="javascript">
<!--
function validInput (form)
{
reg1 = new RegExp("^[A-ZCCŽŠÐa-zccžšd]{4,}");
testing1 = reg1.test(form.user.value);
if (! testing1) {
alert("Username is not valid!");
obrazac.pretplatnik.focus();
return false;
}
reg4 = new RegExp("^[A-ZCCŽŠÐa-zšðèæž0-9]{5,}");
testing4 = reg4.test(form.password.value);
if (! testing4) {
alert("Password must have min. 5 characters!");
form.lozinka.focus();
return false;
}
else
{
return true;
}
}
-->
</script>
How could I get it to work in between those 2 RegExp objects?
I've made the function simpler..
function hasChecked(collection)
{
var elm, k=0;
while(elm=collection[k++])
if(elm.checked)
return true;
return false;
}
eg:
...
if(!hasChecked( form['music[]'] ) )
alert("Must check at least one")