Skip navigation
Thisguy1234
Currently Being Moderated

Help with PDF Javascript calculation involving checkboxes

May 21, 2012 1:47 PM

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

 
Replies
  • George Johnson
    9,209 posts
    Aug 11, 2002
    Currently Being Moderated
    May 21, 2012 2:04 PM   in reply to Thisguy1234

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    May 22, 2012 8:30 AM   in reply to Thisguy1234

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    May 23, 2012 9:18 AM   in reply to GKaiseril

    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;

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points