Limit Checkbox Selections with JavaScript

Limit the number of selected checkboxes

The original explanation uses an alert message when a selection exceeds the rule. Modern examples below also provide inline feedback where practical.

JavaScript can enforce a rule such as “select no more than three.” Count the checked boxes after each change; if a new selection exceeds the limit, undo that selection and show a clear message.

const boxes = [...document.querySelectorAll('input[name="skills"]')];
const maxSelections = 3;
boxes.forEach((box) => {
  box.addEventListener("change", () => {
    const selected = boxes.filter((item) => item.checked);
    if (selected.length > maxSelections) {
      box.checked = false;
      alert("Please select no more than three.");
    }
  });
});

Maximum-count demo getElementsByName() demo Maximum-value demo

Table of Contents

Limit the Number of Selected Checkboxes Top ↑

The original example passes the last-clicked checkbox index into chkcontrol(). A modern event listener already knows which checkbox changed, so it can undo that specific selection directly.

const boxes = [...document.querySelectorAll('input[name="ckb"]')];
const limit = 3;
boxes.forEach((box) => {
  box.addEventListener("change", () => {
    const total = boxes.filter((item) => item.checked).length;
    if (total > limit) {
      box.checked = false;
      alert("Please select only three.");
    }
  });
});

Use getElementsByName() for the Same Rule Top ↑

const boxes = [...document.getElementsByName("ckb")];
const selectedCount = boxes.filter((box) => box.checked).length;

See getElementsByName() for more on that DOM method.

Limit Selection by the Sum of Checkbox Values Top ↑

Limit checkbox selection by total numeric value

Sometimes the count is not the rule. Each checkbox can carry a numeric value and the total may need to remain under a maximum.

const boxes = [...document.querySelectorAll('input[name="ckb"]')];
const maximum = 10;
boxes.forEach((box) => {
  box.addEventListener("change", () => {
    const sum = boxes
      .filter((item) => item.checked)
      .reduce((total, item) => total + Number(item.value), 0);
    if (sum > maximum) {
      box.checked = false;
      alert("The total cannot exceed 10.");
    }
  });
});

Number() converts the control value from a string to a number. The original page used parseInt(), which remains useful when integer parsing is specifically intended.

Give Users Clear Feedback Top ↑

Show the rule before the choices and provide a live count or total where useful. An alert alone should not be the only explanation.

Repeat the Rule on the Server Top ↑

Client-side limits improve interaction but can be bypassed. The server must validate the count or total again before accepting submitted data. The existing PHP checkbox tutorial covers server-side handling.

Limit the number of checkbox selections with JavaScript
Limit checkbox selections by the sum of values

Read selected values · Check/uncheck all · Multiple-selection listbox · Checkbox validation · jQuery data-matrix restrictions

Checkbox selection limits reference




Subscribe to our YouTube Channel here



plus2net.com







KDR

22-12-2010

Do note that the "serial number" (variable n) you assign in the form needs to be sequential in the order the page gets rendered. I.e. when you read your lines of code from the top down, the variable n you assign to each checkbox should be sequential. This only comes into play if you have your checkboxes in a table with more than one column. Your displayed data might go down one column and then down the next, your variable n needs to go left to right, row by row.
semky

04-05-2011

All your check boxes have the same name. when So when php inserts form values into mysql, I need each box to have a separate name. but only 3 can be checked, I am inserting 9 check boxes. check is not like radio with same name.
mdrayhankhan

31-03-2012

Thanks, This code is effective for my work.
Gannear

16-05-2012

Thanx a lot......
Richard

19-06-2012

The demo works very nice, but when I copy and paste the complete code and save as test.html the form is there but does not check how many boxes are checked. Why? thanks.
Sam

14-08-2012

Richard,, same problem !
smo

16-08-2012

Now it is rectified. There was two single quotes on onClick event. Sorry. Try now.
Pete

01-09-2012

Hi, Thanks for the tutorial it was very helpful. I was wondering if that could be converted for text fields? In my form there are 3 text fields but I want the visitor to insert a number in only 1 text field. Would be grateful for your advise, thanks.
phanindra

22-05-2013

plz tell me how to uncheck boxes using forloops /......... I have 18 checkboxes , aLL are checked yes by default when u click on the boxes unchecked then you have to print the numbers side by side in alert ..
smo

22-05-2013

You can little modify the script on Checkbox array and do this. Let us know if it works.
dhawal

20-01-2014

i want same functionality for asp.net checkbox
kkreitz

07-10-2014

Is there a modification to this that allows each checkbox to have a unique value and the user cannot exceed a set sum of these values?

ie. checkbox value=1, checkbox value=.5, checkbox value=1.5, checkbox value=2, sum of boxes checked not to exceed 3.
smo

10-10-2014

Good point. This modification is added with a demo.
Ragith

12-11-2014

Ya good examples. I was very useful for me to use in my codes.
rusty

18-01-2015

My form feeds a database where each checkbox has it's own lengthy name. The checkboxes are also inside of different table names as they correspond to the database. Really, I just need to limit the entire number of checkboxes checked on the form to be no more than 25. Having trouble getting anything I've tried to work. Can you help?
Thiodene

13-07-2015

There is an option in Firefox in the pop up message saying:
"Prevent this page from creating additional dialogs" as soon as you tick this box, this script becomes inactive and you can check all the boxes! That is what I consider a bug!
smo1234

17-07-2015

That is a protection given by browser, so script can't prevent or stop that.
This is not a bug .



✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer