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

Limit all checkboxes between two groups with different limits

New Here ,
Nov 06, 2022 Nov 06, 2022

Copy link to clipboard

Copied

I am creating a PDF form in Acrobat to be used internally and I am hoping there is some JavaScript (or similar code) that can solve my issue. I have never used JavaScript so I am a complete beginner here.

 

I have a total of 10 checkboxes. The first 6 are divided from the last 4 into two groups (visually, I haven't coded them or done anything to group them in acrobat), but they all relate to the same question.

 

I want my clients to be able to select up to 3 of the first 6 checkboxes in what I will call Group 1. They can select less than 3, but no more than 3 checkboxes from Group 1. Selecting any checkboxes in Group 1 means they can not select any checkboxes from the second group of 4, let's call this Group 2.

 

I want my clients to be able to select up to 2 of the last 4 checkboxes in Group 2. They can select less than 2, but no more than 2 checkboxes in Group 2. Selecting any checkboxes in Group 2 means they cannot select any checkboxes from Group 1. If any checkboxes in Group 1 are already selected, they will all deselect as soon as one checkbox is selected in Group 2, and vice versa.

 

We have two issues currently. One is that our clients are selecting more than 3 in the first group or more than 2 in the second group. Our other issue is that our clients are selecting options from both groups. My hope is that there is a script that can solve both of these issues.

 

This post is the most similar solution I could find, but I need many more checkboxes and rules (I assume) for my form: https://community.adobe.com/t5/acrobat-discussions/pdf-form-conditional-checkboxes/m-p/3293678

 

Thank you in advance to anyone willing to help! I hope I explained this clearly. I've attached an image of how the form looks for context but I have hidden the content for privacy reasons.

TOPICS
Create PDFs , How to , JavaScript , PDF forms

Views

921

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

correct answers 1 Correct answer

Community Expert , Dec 17, 2022 Dec 17, 2022

Hi,

Here is an example based on the Souvik's script with a few modifications:

// Function in Document-Level
function limitCheckNumber(prefix, lower, upper, max) {
    // Initialize the selected check box count
    var count=0;
    // Loop through the check boxes to see if the limit has been exceeded
    for (var i=lower; i<=upper; i++) {
        // Increment the counter if the current check box is selected
        if (this.getField(prefix+"."+i).value!="Off") count++;
        // If the count exc
...

Votes

Translate

Translate
Adobe Employee ,
Dec 16, 2022 Dec 16, 2022

Copy link to clipboard

Copied

Hi @nickb9595,

 

Hope you are doing well. Sorry for the delayed response.

 

Per the description, you want to limit the checkbox selection to two different groups.

 

For one part that is limiting the selection to a number of checkboxes; you can use the below script:

 

This script can check each and control what happens when any of the checkboxes are selected. Below is a document-level JavaScript that each check box can call:

    function limitCheckNumber(prefix, lower, upper, max) {

    // Initialize the selected check box count
    var count = 0;

    // Loop through the check boxes to see if the limit has been exceeded
    for (var i = lower; i < upper + 1; i += 1) {

        // Increment the counter if the current check box is selected
        if (getField(prefix + "." + i).value !== "Off") count += 1;

        // If the count exceeds the limit, deselect the check box that was just selected
        // and alert the user
        if (count > max) {
            event.target.value = "Off";
            app.alert("A maximum of " + max + " check boxes can be selected", 3);
            break;
        }
    }
}

And here is the Mouse Up script for each check box:

limitCheckNumber("check", 0, 9, 4);

So in this example, checkboxes are named: check.0, check.1,...,check.9, and it's limited to 4 checkboxes max selected at one time. If you wanted to limit this to just checkboxes 2-8, allowing a maximum of 3 to be selected, the code in each one of these checkboxes would be:

limitCheckNumber("check", 2, 8, 3);

and don't use any script in check.0, check.1, and check.9

 

Since we are not entirely trained in Scripting, I am adding the appropriate tags to the post so that our community experts can have better visibility of the post and add their valuable comments.

 

You can take reference from the help article to know more on how to use JavaScripts on PDF: Applying actions and scripts to PDFs

 

Hope this helps.

 

-Souvik.

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 ,
Dec 17, 2022 Dec 17, 2022

Copy link to clipboard

Copied

Hi,

Here is an example based on the Souvik's script with a few modifications:

// Function in Document-Level
function limitCheckNumber(prefix, lower, upper, max) {
    // Initialize the selected check box count
    var count=0;
    // Loop through the check boxes to see if the limit has been exceeded
    for (var i=lower; i<=upper; i++) {
        // Increment the counter if the current check box is selected
        if (this.getField(prefix+"."+i).value!="Off") count++;
        // If the count exceeds the limit, deselect the check box that was just selected
        // and alert the user
        if (count>max) {
            event.target.value="Off";
            app.alert("A maximum of "+max+" check boxes can be selected", 3);
            break;
        }
    }
}

// upTo3 check boxes
limitCheckNumber("upTo3",0,5,4);

// upTo2 check boxes
limitCheckNumber("upTo2",0,3,2);

@+

 

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 ,
Dec 18, 2022 Dec 18, 2022

Copy link to clipboard

Copied

LATEST

Hi,

As you will have certainly rectified, you must call the function for the first group with:

limitCheckNumber("upTo3",0,5,3);

@+

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