Skip to main content
Participating Frequently
March 16, 2018
Answered

I need to calculate a discounted rate if 2 of 4 options are selected on a form.

  • March 16, 2018
  • 1 reply
  • 1446 views

I am developing an entry form. There are four options per entry line. If two of the four options are selected, there is an entry discount. What is the best way to calculate the discount?

For example: the options are A, B, C, D. A & B export a value of 7, C & D export a value of 3. If A AND B are selected, then they should be 6.5 or:

If A OR B, then SUM (A+B+C+D);

If A AND B, then SUM (A+B+C+D-1)

This topic has been closed for replies.
Correct answer try67

Ok. Still frustrated. I made the adjustments you suggested, and it's still not calculating the values. Here is the link to the file. It's in a DropBox folder: https://1drv.ms/b/s!AlZCRMmM6fhpgWU0rgRw80vquQ_8

My code is in the first line, and the other lines just have the simple code. Your suggestions are very much appreciated. I think JavaScript hates me.


OK, you have several issues with both the code and the set-up of the fields.

First of all, you were still using incorrect variable names.

Also, when the check-boxes are not ticked the value they have is "Off", which can't be converted to a number, so it results in an error.

So use this code:

var oSat1 = (this.getField("Sat1").valueAsString=="Off") ? 0 : Number(this.getField("Sat1").valueAsString);

var oSun1 = (this.getField("Sun1").valueAsString=="Off") ? 0 : Number(this.getField("Sun1").valueAsString);

var oSaF1 = (this.getField("SaF1").valueAsString=="Off") ? 0 : Number(this.getField("SaF1").valueAsString);

var oSuF1 = (this.getField("SuF1").valueAsString=="Off") ? 0 : Number(this.getField("SuF1").valueAsString);

if (oSat1 && oSun1) event.value = (oSat1 + oSun1 + oSaF1 + oSuF1 - 1);

else if (oSat1 || oSun1) event.value = (oSat1 + oSun1 + oSaF1 + oSuF1);

else event.value = 0;

In addition, you have to remove the "$" symbol from the export values of all the check-boxes. For example, the value of "Sat1" was "$7.00". That also can't be converted to a number, so change it to just "7".

1 reply

Thom Parker
Community Expert
Community Expert
March 16, 2018
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
March 28, 2018

I have been trying this over and over and not getting anywhere. Here is the script I have right now:

var nFee1 = this.getField("Fee1").value;

if( (Sat1) && (Sun1) ) event.value = ( (Sat1) + (Sun1) + (SaF) + (SuF) - 1 );

else if( (Sat1) || (Sun1) ) event.value = ( (Sat1) + (Sun1) + (SaF) + (SuF) );

My form has checkboxes, and each checkbox has a value to export (Sat1 = 7; Sun1 = 7; SaF = 3; SuF = 3). No matter what I try, I can't get the custom calculation box to read a value. I have even tried to set the values in the calculation box. What am I missing? Again, if both "Sat1" and "Sun1" are selected, the calculation should subtract 1.

try67
Community Expert
Community Expert
March 28, 2018

Is that your full code? If so, you're not accessing the fields' values correctly (except for "Fee1", which you're not actually using)...