4 Replies Latest reply on May 23, 2012 9:18 AM by gkaiseril

    Help with PDF Javascript calculation involving checkboxes

    Thisguy1234

      Hello Everyone,

       

      I have a PDF Form, and need help with a calculation.

       

      For the sake of simplicity, Lets say I have 5 fields. FSA1 and FSA2 are checkboxes. TotalClaimed1 and TotalClaimed2 are text fields which allow only a numerical input.

       

      The last box, FSA_Total, should add the value from TotalClaimed1 if FSA1 is checked, and it should also add the value from TotalClaimed2 if FSA2 is checked.

      ---------------

       

      The code is malfunctioning, however. Something is causing it to add to the total every time the box is checked. Its almost like the checking off of the box is what causes the addition.

       

      It seems like the code is being read as "When the box is checked, add the value to the total", so each time the box gets checked, it adds to the total again.

      and it should be "IF the box is checked, add the value to the total" so if there is a value in the field, and the box is checked, that value is passed on to the total; and if the box is not checked, the value is ignored.

       

      Can anyone spot what might be causing this? Code is below.

       

      ---------------

       

      var claim1 = parseInt(this.getField('TotalClaimed1').value),

          claim2 = parseInt(this.getField('TotalClaimed2').value),

          fsabox = parseInt(this.getField('FSA_Total').value);

       

      if (this.getField('FSA1').value == 'Yes') {

          fsabox += claim1;

      }

       

      if (this.getField('FSA2').value == 'Yes') {

          fsabox += claim2;

      }

       

      event.value = fsabox;

       

      -------------------

       

      Thanks

       

      Thisguy1234

        • 1. Re: Help with PDF Javascript calculation involving checkboxes
          George_Johnson MVP & Adobe Community Professional

          This is a custom calculation script for FSA_Total, correct? The code is working correctly, it's just not what you want. Assuming this is a custom calculation script in the FSA_Total field, it should be something like:

           

          var sum = 0;

          var claim1 = parseInt(getField('TotalClaimed1').value, 10),

              claim2 = parseInt(getField('TotalClaimed2').value, 10),

           

          // Add field values if corresponding check boxes are selected

          if (getField('FSA1').value !== "Off") {

              sum += claim1;

          }

           

          if (getField('FSA2').value !== "Off") {

              sum += claim2;

          }

           

          // Set this field value

          event.value = sum;

           

           

          Why are you using parseInt, exactly?

          • 2. Re: Help with PDF Javascript calculation involving checkboxes
            Thisguy1234 Level 1

            I used parseInt because it kept adding the digits as strings... like 10 + 10 would be 1010.

             

            I found the problem in the code. I had to change

            fsabox = parseInt(this.getField("FSA_Total").value);

             

            to  fsabox = 0;

             

            Of course, that was saying get whatever is in FSA_Total right now and start adding to it. I wanted to start from 0 each time, and add to that.

             

            Thanks so much for the quick reply!

            • 3. Re: Help with PDF Javascript calculation involving checkboxes
              gkaiseril MVP & Adobe Community Professional

              You have to make sure you are trying to add numbers and not charactered string values. The "+" operator is used for both addition and concatenation. You can use the Number constrictor or the multiplicative identity to force a value to a number value or "NaN" value. You can use "isNaN(value)" to test for NaN values.

              • 4. Re: Help with PDF Javascript calculation involving checkboxes
                gkaiseril MVP & Adobe Community Professional

                Have you tried forcing your input values to a number before applying the "parseInt()" function.

                 

                var claim1 = parseInt(Number(this.getField('TotalClaimed1').value,10)),

                    claim2 = parseInt(Number(this.getField('TotalClaimed2').value,10)),

                    fsabox = parseInt(Number(this.getField('FSA_Total').value,10));

                if(isNaN(fsabox )) {

                fsabox = 0;

                }

                if (this.getField('FSA1').value == 'Yes') {

                    fsabox += claim1;

                }

                if (this.getField('FSA2').value == 'Yes') {

                   fsabox += claim2;

                }

                event.value = fsabox;