Introduction

This tutorial shows how to limit number of checkbox selections using JavaScript in a group of checkboxes. Suppose you have a group of checkboxes in a form or in a div or table cell or wherever else and you have a requirement that a user can select maximum of two or three or n number of checkboxes then you need to restrict it using javascript or jquery. The below example shows how to achieve it using JavaScript.

Prerequisites

Knowledge of JavaScript

Example with Source Code

We will show here two approaches on how to limit number of checkbox selections using JavaScript.

Using Form

The below example shows that maximum of two checkboxes can be selected. The checkboxes are put inside an HTML form.

HTML

In the below source code we have added three checkboxes in the form and we will allow user selects only two checkboxes out of three.

Notice how we have given name with array for each checkbox. The below code snippets should be put inside HTML body.

<h1>Using Form</h1>
<form method="post" name="form_name" id="form_name">
	<input type="checkbox" name="check[]" value="CheckBox 1"/>
	<input type="checkbox" name="check[]" value="CheckBox 2"/>
	<input type="checkbox" name="check[]" value="CheckBox 3"/>
</form>
<script type="text/javascript">checkBoxLimit()</script>

JavaScript

The below source code is used to limit checkbox selections using JavaScript in the above html form. We log into the console as well as we show alert notification to the end users.

Put the below code snippets under HTML <head> tag.

function checkBoxLimit() {
	var checkBoxGroup = document.forms['form_name']['check[]'];			
	var limit = 2;
	for (var i = 0; i < checkBoxGroup.length; i++) {
		checkBoxGroup[i].onclick = function() {
			var checkedcount = 0;
			for (var i = 0; i < checkBoxGroup.length; i++) {
				checkedcount += (checkBoxGroup[i].checked) ? 1 : 0;
			}
			if (checkedcount > limit) {
				console.log("You can select maximum of " + limit + " checkboxes.");
				alert("You can select maximum of " + limit + " checkboxes.");						
				this.checked = false;
			}
		}
	}
}

Using Div

You can place checkboxes in any tag using attribute id. The below example shows that only one checkbox can be selected.

HTML

Here in the below source code we have added three checboxes and only one checkbox will be selected.

We have put the checkboxes inside HTML <div>. Put the below code snippets inside HTML <body> tag.

<h1>Using Div</h1>
<div id="checkboxgroup">
	<input type="checkbox" value="CheckBox 1" name="check[]">
	<input type="checkbox" value="CheckBox 2" name="check[]">
	<input type="checkbox" value="CheckBox 3" name="check[]">
</div>
<script type="text/javascript">
	onlyOneCheckBox()
</script>

JavaScript

The below source code restricts user to limit checkbox selections using JavaScript. Here you can select only one checkbox.

function onlyOneCheckBox() {
	var checkboxgroup = document.getElementById('checkboxgroup').getElementsByTagName("input");
	var limit = 1;
	for (var i = 0; i < checkboxgroup.length; i++) {
		checkboxgroup[i].onclick = function() {
			var checkedcount = 0;
				for (var i = 0; i < checkboxgroup.length; i++) {
				checkedcount += (checkboxgroup[i].checked) ? 1 : 0;
			}
			if (checkedcount > limit) {
				console.log("You can select maximum of " + limit + " checkbox.");
				alert("You can select maximum of " + limit + " checkbox.");
				this.checked = false;
			}
		}
	}
}

Testing the Application

Using Form

limit number of checkbox selections using javascript

Using Div

limit number of checkbox selections using javascript

Source Code

You can download source code.

Thanks for your reading.

Tags: •

Leave a Reply

Your email address will not be published. Required fields are marked *