11 Replies Latest reply on Aug 20, 2013 7:34 AM by AdobNewbee

# How do I code a calculation based from a value field & a Checkbox?

How do I get a field to calculate a commission based on another field (we will call it "Total Amount" for this example) and multiply it with the corrisponding % depending on a check box.  I have 3 different series that are check boxes. Depending on which one a client chooses, "Total Amount" would be multiplied by either 8%, 3% or 0% to get our total commission

• ###### 1. Re: How do I code a calculation based from a value field & a Checkbox?

Let's assume you have the following fields:

• 3 check boxes all called "Rate", but with export values of 8, 3, and 0
• A text field called "Total Amount" which has a number to be multiplied by the rate
• A text field called "Commission" which multipies the "Rate" and "Total Amount"

Set the following as the custom calculation script of the "Commission" text field:

var v = this.getField("Rate").value;

var t = this.getField("Total Amount").value;

event.value = v * t/100;

I hope this helps.

• ###### 2. Re: How do I code a calculation based from a value field & a Checkbox?

Thank you Michael!!

Quick question though....how do I tie the appropriate value to the applicable check box so your formula caluclates? Check BoxA = 8%, Check BoxB = 3%, Check BoxC = 0%

• ###### 3. Re: How do I code a calculation based from a value field & a Checkbox?

Check the first bullet in Michael's answer - you give all of the checkboxes the same name ('Rate' in his example), and set the export value for each checkbox accordingly (8, 3, 0). The export value is what is used for the calculations.

• ###### 4. Re: How do I code a calculation based from a value field & a Checkbox?

Sorry, I missed that, thank you for pointing it out!

I've done that and put the calculation in and I'm getting the NaN error.  I'm using Adobe 9 Pro, don't think that should make a difference but even when I do a simple calculation using the Value option, it isn't working for me....what am I missing?  I have tried changing the Format to "Number" but then nothing shows up.

Thank you, really appreciate the help on this one!!

• ###### 5. Re: How do I code a calculation based from a value field & a Checkbox?

Figured it out

I didn't have the "Total Amount" field set at a "Number" for the format.

Thank you all for your help, truly appreciate it!!

• ###### 6. Re: How do I code a calculation based from a value field & a Checkbox?

Me, yet again....

I have the follow error coming up "The value entered does not match the format of the field [Commission]"

What script do I need to add so this doesn't keep popping up if a client doesn't choose a check box right away?

• ###### 7. Re: How do I code a calculation based from a value field & a Checkbox?

The "Commission" and "Total Amount" text fields should have a format of "None"

• ###### 8. Re: How do I code a calculation based from a value field & a Checkbox?

When I mark them both as "None" for the format, the "Commission" field is calculated as "NaN"

• ###### 9. Re: How do I code a calculation based from a value field & a Checkbox?

When none of the Rate check boxes are selected, the field value will be equal to the string "Off". When you multiply that string value by a number, you get the special value of NaN, meaning "not a number". So your code should first check the value and only do the multiplication if the value is not "Off". You also have to decide what the calculated value should be when it is "Off".

• ###### 10. Re: How do I code a calculation based from a value field & a Checkbox?

Good point George.

AdobNewbee, modify the script to be:

//Set as the custom calculation script of the text field

var v = this.getField("Rate").value;
var t = this.getField("Total Amount").value;
if(v!=="Off"){
event.value = v * t/100;
}
else
event.value = "";

• ###### 11. Re: How do I code a calculation based from a value field & a Checkbox?

Works beautifully!!  Thank you so much Michael and George, you have saved me