Java Script Calculation in PDF Form

Community Beginner ,
Apr 28, 2021 Apr 28, 2021

Copy link to clipboard

Copied

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

 

TOPICS
JavaScript

Views

137

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Community Beginner , Apr 28, 2021 Apr 28, 2021
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 st...

Likes

Translate

Translate
Engaged ,
Apr 28, 2021 Apr 28, 2021

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 28, 2021 Apr 28, 2021

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 28, 2021 Apr 28, 2021

Copy link to clipboard

Copied

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;

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 28, 2021 Apr 28, 2021

Copy link to clipboard

Copied

LATEST

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines