Subject: Javascript code for checkbox validation
Author: WebSpider
Posted on: 03/10/2009 08:49:32 PM
If, for example, you have a multiple checkboxes in a form and want to pop up an alert if none of them was checked, you can do it with the aid of property checked like the one below:
<SCRIPT type="text/javascript">
function checkCheckBoxes() {
if (document.getElementById("checkbox_dog").checked == false &&
document.getElementById("checkbox_cat").checked == false )
{
alert ('You did not choose any of the checkboxes yet.');
return false;
} else {
return true;
}
}
</SCRIPT>
<html>
<body>
<form onsubmit="return checkCheckBoxes();" action="">
<input type="checkbox" id="checkbox_dog" value="Dog">Dog</p>
<input type="checkbox" id="checkbox_cat" value="Cat">Cat</p>
<input type="submit" value="Submit" />
</form>
</body>
</html>
References: