Validating Checkboxes

Contact Us or call 1-877-932-8228
Validating Checkboxes

Validating Checkboxes

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

Code Sample:

<meta charset="UTF-8">
<script type="text/javascript">
function validate(form){
	var errors = [];
	if ( !checkCheckBox(form.terms) ) {
		errors[errors.length] = "You must agree to the terms.";
	if (errors.length > 0) {
		return false;
	return true;

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];
<h1>Ice Cream Form</h1>
<form method="post" action="Process.html" onsubmit="return validate(this);">
	<input type="checkbox" name="terms">
	I understand that I'm really not going to get any ice cream.
	<input type="submit" value="Place Order">


We'll go over this code in more details in the upcoming presentation