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

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

Community Beginner ,
Jul 16, 2018 Jul 16, 2018

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);

TOPICS
Acrobat SDK and JavaScript
10.7K
Translate
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 , Jul 17, 2018 Jul 17, 2018

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

...
Translate
Community Expert ,
Jul 17, 2018 Jul 17, 2018

I don't see where you inserted my code. It needs to come instead of

everything that you have at the doc-level. Everything else can stay the

same.

Translate
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 Beginner ,
Jul 17, 2018 Jul 17, 2018

I got it! Thank you very much for your help!!!

Translate
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 Beginner ,
Jul 17, 2018 Jul 17, 2018

Counter needs to start at -1 otherwise only 4 are selected and message pops at the 5th.

Translate
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 ,
Jul 17, 2018 Jul 17, 2018

No, don't do that. Instead, change this line:

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

To:

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

Translate
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 Beginner ,
Jul 17, 2018 Jul 17, 2018

I reset counter value to 0. But I can only select 3 when adding the =

Translate
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 Beginner ,
Jul 17, 2018 Jul 17, 2018

I found my mistake. Thanks!

Translate
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 ,
Jul 17, 2018 Jul 17, 2018

You can actually remove this part of the code:

if (value == "Yes"){

    counter++;

}   

Translate
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 Beginner ,
Jul 17, 2018 Jul 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.

Translate
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 ,
Jul 17, 2018 Jul 17, 2018

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.

Translate
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 Beginner ,
Jul 17, 2018 Jul 17, 2018

That's it! I need to be able to check the fifth box. Hence the -1.

Translate
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 ,
Jul 17, 2018 Jul 17, 2018

OK, then your description was not clear... Anyway, if it works leave it as is, although starting from -1 is not the best practice.

Translate
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 Beginner ,
Jul 17, 2018 Jul 17, 2018
LATEST

It works perfectly!

Translate
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 ,
Jul 17, 2018 Jul 17, 2018

OK.

You should look at the console for error messages.

Translate
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 Beginner ,
Jul 17, 2018 Jul 17, 2018

The issue I am having is the checked boxes counter reset to 0 every time I reopen this saved file and check new boxes. When I want it to retain the value of this.getField("Quantity_Produits_Finale").value (lets say 5) and stop people from checking new boxes.

So in other words on first go it stops at 5. OK
I save and reopen file
It allows me to check another 5 boxes when it should simply stop the user from checking more than 5 boxes NOT OK

I simply don't know how to implement that correctly?

Translate
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 ,
Jul 17, 2018 Jul 17, 2018

Your code at file open is incorrect. Look at the error message.

Translate
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 Beginner ,
Jul 17, 2018 Jul 17, 2018

Ok! How do I fix this?

Translate
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 ,
Jul 17, 2018 Jul 17, 2018

Change the code.

Translate
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