Skip to main content
Participant
April 28, 2021
Answered

Java Script Calculation in PDF Form

  • April 28, 2021
  • 1 reply
  • 2152 views

Hello there

 

This might be easy for someone: 

I'd like to calculate in a PDF Form. 

 

I have 2 components which are relevant for the calc: 

1. Amount (whole numbers)

2. Checkbox (if this is checked, the amount should be multiply by 16 insted of 14)

 

There should be a condition: 

if the amount is between 2 and 4, it should multiply by 0.85

if the amount is between 5 and 9, it sould multyply by 0.75

if the amount is 10 or greater than 10 it should multyply by 0.50 

 

Any ideas? 

Would be so helpful. 

 

Best P

 

This topic has been closed for replies.
Correct answer Pascale5FCE

If I understand  right, try use this code as calculation script of "Result" field:

var check = this.getField("Checkbox").valueAsString;
var num = Number(this.getField("Amount").value);
var x = 0;
var y = 0;
if(check == "Off")
x = 14;
else if(check != "Off")
x = 16;
if(num >= 2 && num <=4)
y = (num*x)*0.85;
else if(num >= 5 && num <=9)
y = (num*x)*0.75;
else if(num >= 10)
y = (num*x)*0.50;
else y = num*x;
event.value = y;

 


Hi Nesa

 

Thanks for your reply. 

 

I somehow managed to do it like that:

var amount = this.getField("amount").value; 
var checkbox = this.getField("checkbox");

if (amount === 1) 
{ 
if (checkbox.value != "Off") {
this.getField("total").value = amount*16; 
} else {
this.getField("total").value = amount*14
}
} 

else 
{ 
if (amount >= 2) 
{ 
if (checkbox.value != "Off") {
this.getField("total").value = amount*16*0.85; 
} else {
this.getField("total").value = amount*14*0.85
}
}

else 

// with more steps for higher numbers and >= arguments 
}
}  

 

I tested your code and it worked perfectly as well! Thank you 🙂

 

Best Pascale

1 reply

Inspiring
April 28, 2021

When you say if checkbox is checked amount is multiply by 16 instead of 14, that means if checkbox is not checked amount is multiply by 14?

So where does then other conditions comes to play?

All of this happening in one field?

Participant
April 28, 2021

Hi Asim

 

Thanks for the reply

 

With the status checkbox off, the calc should be

Amount (e.g. 5) * 14 = 70

 

With the checkbox selected, it should switch to

Amount (e.g. 5) * 16 = 80 

 

And this sould be multiplied by 0.75 (because 5 is in between 5-9).

 

I work with 3 Fields:

1. Amount (Textfield for the numbers)

2. Checkbox Switch (for the switch from 14 to 16)

3. Result (Textfield with can't be edited)

 

 Best Pascale

Nesa Nurani
Community Expert
Community Expert
April 28, 2021

If I understand  right, try use this code as calculation script of "Result" field:

var check = this.getField("Checkbox").valueAsString;
var num = Number(this.getField("Amount").value);
var x = 0;
var y = 0;
if(check == "Off")
x = 14;
else if(check != "Off")
x = 16;
if(num >= 2 && num <=4)
y = (num*x)*0.85;
else if(num >= 5 && num <=9)
y = (num*x)*0.75;
else if(num >= 10)
y = (num*x)*0.50;
else y = num*x;
event.value = y;