Skip to main content
Known Participant
July 16, 2018
Answered

Interactive PDF form - How to stop script from resetting on reopen?

  • July 16, 2018
  • 4 replies
  • 10276 views

I have a script that counts the checked boxes up to a predefined number (lets say 5) and stops users from checking more boxes. But when I close and reopen I can check another 5. Is there a way to prevent this from happening?

//----------------------------Document JavaScript Code ----------------------------

var counter = 0;                 // Checked counter

//--------Count Checked boxes again when document is closed and re-opened ------------

for (var i =0; i<= 100; i++){

          if (getField("chkBox" + i).value == "Yes"){

          counter += 1;

          }

}

//-------------Validation Function ---------------------------------------------------

function validateCheckBox(name,value){

          if (value == "Yes" && counter < this.getField("Quantity_Produits_Finale").value){

                    counter += 1;

          }else if (value == "Off"){

                    counter -= 1;

          }else{

                    getField(name).value = "Off";

                    app.alert("Vous ne pouvez pas sélectionner plus de " + this.getField("Quantity_Produits_Finale").value + " produit(s)");

          }

}

//-----------------End of Document Javascript------------------------

Action on each checkbox

validateCheckBox(event.target.name,event.target.value);

This topic has been closed for replies.
Correct answer try67

Does your solution account for check boxes that are the same name but different iteration? As they only count for one checked box. I will see if I can implement correctly.


Yes, it does. Here's the latest version of my code, it will work even with the gaps in your field names:

function validateCheckBox(name, value){

    var counter = 0;

  

    for (var i=0; i<this.numFields; i++) {

        var f = this.getField(this.getNthFieldName(i));

        if (f==null) continue;

        if (f.type=="checkbox" && /^0 Check Box/.test(f.name) && f.value=="Yes") {

            counter++;

        }

    }

  

    if (value == "Yes"){

        counter++;

    }  

    if (counter>Number(this.getField("Quantity_Produits_Finale").value)) {

        this.getField(name).value = "Off";

        app.alert("Vous ne pouvez pas sélectionner plus de " + this.getField("Quantity_Produits_Finale").value + " produit(s)");

    }

}

4 replies

BarlaeDC
Adobe Expert
July 16, 2018

HI,

Can you share the file, using a file sharing service, as I said earlier, your code with no changes works for me, I have a document with 101 check boxes, and I am unable to select more than 5. So I am not sure what the problem is.

Regards

Malcolm

Known Participant
July 16, 2018

Here is the link to file Dropbox - Form_Kit_v20_FR_test.pdf

try67
Adobe Expert
July 17, 2018

Even with >= and code removal counter needs set to -1. It is when user selects the 6th box that error message pops up.


Works fine for me...

function validateCheckBox(name,value){

    var counter = 0;

  

    for (var i=0; i<this.numFields; i++) {

        var f = this.getField(this.getNthFieldName(i));

        if (f==null) continue;

        if (f.type=="checkbox" && /^0 Check Box/.test(f.name) && f.value=="Yes") {

            counter++;

        }

    }

    if (counter>=Number(this.getField("Quantity_Produits_Finale").value)) {

        this.getField(name).value = "Off";

        app.alert("Vous ne pouvez pas sélectionner plus de " + this.getField("Quantity_Produits_Finale").value + " produit(s)");

    }

}

When clicking the fifth box, the error message appears.

try67
Adobe Expert
July 16, 2018

Also, you need to separate the check of the counter against the value of the maximum allowed number and the part that counts up the number of fields. The two things are not related and should not be a part of the same if-condition.

Known Participant
July 16, 2018
6. Re: Interactive PDF form - How to stop script from resetting on reopen?

try67MVP & Adobe Community Professional

//--------Count Checked boxes again when document is closed and re-opened ------------

+ //-------------Validation Function ---------------------------------------------------

try67
Adobe Expert
July 16, 2018

There's no need for a doc-level variable. Also, you should not reduce the value of the counter by 1 if the field is not ticked. It should only count the checked fields.

BarlaeDC
Adobe Expert
July 16, 2018

HI,

The code works for me without changes, although I believe you need to keep the counter -= 1, as this would be required if the user changes their mind, and therefore goes below the limited number of checkboxes.

Can I check a simple thing, how many checkboxes do you have? is it 101?

Regards

Malcolm

Known Participant
July 16, 2018
3. Re: Interactive PDF form - How to stop script from resetting on reopen?

BarlaeDCAdobe Community Professional

I do have more than 100 checkboxes. So I will up the limit. Thanks for pointing that out!

Inspiring
July 16, 2018

I would begin troubleshooting by checking the value in the counter variable when the document is opened. Is it what you expect?

Known Participant
July 16, 2018

The counter variable resets to zero upon reopening of the document when it should be 5.