• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Limit the number of checkboxes that can be selected

Explorer ,
Mar 16, 2016 Mar 16, 2016

Copy link to clipboard

Copied

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!

TOPICS
Acrobat SDK and JavaScript

Views

2.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2016 Mar 16, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 23, 2016 Mar 23, 2016

Copy link to clipboard

Copied

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.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 25, 2023 May 25, 2023

Copy link to clipboard

Copied

I could not get the pdf to open

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 25, 2023 May 25, 2023

Copy link to clipboard

Copied

There is no need for script in each checkbox, it can be done by using calculation script in a text field.

Can you explain what you try to achieve exactly?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 26, 2023 May 26, 2023

Copy link to clipboard

Copied

The web site is not more available.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 11, 2023 Sep 11, 2023

Copy link to clipboard

Copied

LATEST
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.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines