Skip to main content
Dreamy_77
Known Participant
March 16, 2016
Question

Limit the number of checkboxes that can be selected

  • March 16, 2016
  • 2 replies
  • 3833 views

Hi there - I'm working on a questionnaire form - and need to limit the user to be able to select a maximum of three checkboxes (from a total of 16) on one of the questions.

Any help on how to do this would be much appreciated!

This topic has been closed for replies.

2 replies

Known Participant
March 24, 2016

Consider a Petri Net model for limiting the number of checks in a set of checkboxes (Chionglo, 2016); the following solution or control logic is based on the Petri Net model:

 

  1. A special variable (nc) is needed to keep track of the number of checks still allowed.
  2. A special variable (ncLimit) is needed for the total number of simultaneous checks allowed.
  3. The initial state of the system: no checks and nc=ncLimit.
  4. The MouseUp event of every checkbox should have the following update logic:
    1. When a check event occurs
      1. Deduct 1 from nc.
      2. If nc < 1 then set readonly=true for every checkbox that is still unchecked.
    2. When an uncheck event occurs
      1. Add 1 to nc.
      2. If nc<2 then set readonly=false for every checkbox that is still unchecked.

[A PDF version of this reply includes two sample forms that obey the checkbox limit using the control logic described above: http://www.aespen.ca/AEnswers/zngeW1458711009.pdf ]. 

References

 

Chionglo, J. F. (2016). “A reply to ‘limit the number of checkboxes that can be selected’” at Adobe, PDF Forms Discussions. Available at http://www.aespen.ca/AEnswers/zlEsm1458624106.pdf.

 

Participant
May 25, 2023

I could not get the pdf to open

 

Participant
September 11, 2023
I answered this question on Mar. 23, 2016 under the username "John Frederick Chionglo". However the email I used for that account does not exist anymore and I forgot the password to that account. So I am modifying the answer via this reply under the username "John F. Chionglo".
 
For this example assume there are six checkboxes (named CheckBox1, CheckBox2, CheckBox3, CheckBox4, CheckBox5 and CheckBox6). Add the following code as a document-level script.
 

 

 

var ncLimit = 3;
var nc = ncLimit;
function freezeUncheckedCheckboxes(dc) {
  var fld, j;
  for (j=1; j<6; j++) {
    fld = dc.getField("CheckBox" + j);
    if (fld.value=="Off") {
      fld.readonly = true;
    }
  }
}
function unfreezeCheckboxes(dc) {
  var fld, j;
  for (j=1; j<6; j++) {
    fld = dc.getField("CheckBox" + j);
    if (fld.readonly) {
      fld.readonly = false;
    }
  }
}

 

 

 
Add the following to the MouseUp event of CheckBox1, ... CheckBox6.
 

 

switch(event.target.value) {
  // check event
  case "Yes":
    nc -= 1;
    if (nc<1) { freezeUncheckedCheckboxes(this); }
    break;
  // uncheck event
  case "Off":
    nc += 1;
    unfreezeCheckboxes(this);
    break;
  default:
    console.show();
    console.println("Error checkbox (" + event.target.name
      + ") value. Unrecognized value ... '" + event.target.value + "'. Must be 'Yes' or 'Off'.");
    break;
}

 

 
The attachment is an example implementation of this recommendation.

 

try67
Community Expert
Community Expert
March 16, 2016

This requires a complex script which needs to be attached to the Mouse Up event of each check-box. The script should iterate over all of the boxes in the list, counting how many of them are ticked. If the maximum number has been reached it should change the value of the box that triggered it to be un-ticked, and possibly show an error message.