Skip to main content
Participant
July 23, 2020
Question

Checkboxes : Max of 5 checked on a total of 28

  • July 23, 2020
  • 3 replies
  • 545 views

Hello ! I need to find a way to control the number of check marks in a form made in Indesign. I want a max of 5 boxes checked on a total of 28. Is there a way ?!? I know nothing about scripts... 😞  Hope anyone can help ! Thanks !

This topic has been closed for replies.

3 replies

Thom Parker
Community Expert
Community Expert
July 26, 2020

Malcolm has the right idea. The number of checks need to be tested on each selection, and then prevented when the max is exceeded. A folder level MouseUp fuction is one place to put the script. However, since there are several checkboxes, I would suggest a calculation script on a hidden text field. Calculation scripts are called anytime any field on the form is changed. This will allow you to call the script in a single location, rather than having to put the call in several locations. This is much eassier to maintain, but it really doesn't matter as far a functionality goes.

I would also suggest a surther modification of this strategy. And this one is important. 

 

First, name all of the check boxes with group naming. Group naming provides an easy way to identify and process all of the checkboxes. For example, "Check.choice1", "Check.choice2", etc. 

 

Now this document level fuction will do the trick.

 

 

function ValidateCheckTotal()
{
    if(event.source.name.split(".").shift() == "Check")
    {
        var nCount = 0;
        this.getField("Check").getArray().forEach(function(a){if(a.value != "Off")nCount++;});
        this.calculate = false;
        if(nCount > 5) event.source.value = "Off";
        this.calculate = true;
    }
}

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
BarlaeDC
Community Expert
Community Expert
July 25, 2020

Hi,

 

This is not as easy as you would hope, as if the user saves inbetween filling in the fields it can add complications, I would create a Document level JavaScript that is run by each checkbox that gets the total number that have been checked and if that number if above 5, show an error to the user ( and if you want it to be smart, uncheck the last checkbox checked). This would enable each checkbox to use the same code, therefore reducing the complexity of the script.

function checkTotalCheckboxes( fieldName)
{
    var curTotal = 0;
    var numFields = this.numFields;
    for ( var i = 0; i < numFields; i++){
        var curField = this.getField(this.getNthFieldName(i));
        if ( curField.type == "checkbox"){
              if ( curField.isBoxChecked(0)){
                   curTotal++;
              }
              if ( curTotal > 3) {
                    curField.checkThisBox(0, false);
                    app.alert ("too many checkboxes checked");
              }
          }
     }
}

 

The above is a sample script ( not fully tested) but should give you a starting point.

 

Regards

 

Malcolm

 

ls_rbls
Community Expert
Community Expert
July 24, 2020

The link below has a similar discussion to your inquiry.

 

Even though the  various solutions focus on Acrobat javascript I think it may be useful for you to get a visual on how to use javascript to handle different scenarios where numerous checkboxes are used.

 

It would be helpful if you describe in more detail what is the intent of your PDF and how the users will interact with the form.

 

In any case, I think that in your particular case a validation script should be used, but you need to provide more info about the workflow that you're trying to achieve.

ls_rbls
Community Expert
Community Expert
July 25, 2020