Validating Check Boxes

Contact Us or call 1-877-932-8228
Validating Check Boxes

Validating Check Boxes

Like radio buttons, check boxes have the checked property, which is true if the button is checked and false if it is not. However, unlike radio buttons, check boxes are not stored as arrays. The example below shows a simple function for checking to make sure a check box is checked.

Code Sample:

<meta charset="UTF-8">
function validate(e){
	var errors = [];
	if ( !checkCheckBox(document.getElementById("terms")) ) {
		errors[errors.length] = "You must agree to the terms.";
	if (errors.length > 0) {

}function checkCheckBox(cb){
	return cb.checked;

function reportErrors(errors){
	var msg = "There were some problems...\n";
	var numError;
	for (var i = 0; i<errors.length; i++) {
		numError = i + 1;
		msg += "\n" + numError + ". " + errors[i];

window.onload = function() {
	document.getElementById("noicecreamform").addEventListener("submit", function(e){
<h1>Ice Cream Form</h1>
<form method="post" action="Process.html" id="noicecreamform">
	<input type="checkbox" name="terms" id="terms">
	I understand that I'm really not going to get any ice cream.
	<input type="submit" value="Place Order">


Code Explanation