hi all
i have one form in which i have multiple checkbox options for the user to select.
i have inserted the validation script in this form which alert the user if the user has not selected any of the checkbox options. if one checkbox is selected then the form will get submit.
now i want that user should select atleast 4 checkboxes. plz help me with what to change or add in this script.
the form and javascript validation code is below :
<html>
<head>
<script language=“javascript”>
function validate()
{
var chks = document.getElementsByName(‘colors[]’);
var hasChecked = false;
for (var i = 0; i < chks.length; i++)
{
if (chks*.checked)
{
hasChecked = true;
break;
}
}
if (hasChecked == false)
{
alert(“Please select at least one.”);
return false;
}
return true;
}
</script>
</head>
<body>
<form name=“form1” onSubmit=“return validate()”>
Your colors are <br>
<input type=“checkbox” name=“colors[]” value=“blue” id=“blue”>Blue <br>
<input type=“checkbox” name=“colors[]” value=“red” id=“red”>red <br>
<input type=“checkbox” name=“colors[]” value=“green” id=“green”>green <br>
<input type=“checkbox” name=“colors[]” value=“yellow” id=“yellow”>yellow <br>
<input type=“checkbox” name=“colors[]” value=“voilet” id=“voilet”>voilet <br>
<input type=“checkbox” name=“colors[]” value=“grey” id=“grey”>grey <br>
<br>
<input type=“submit” value=“submit” name=“submit”/>
</form>
</body>
</html>