Skip to main content
New Participant
January 20, 2023
Answered

Calculate on button click plus verify total

  • January 20, 2023
  • 1 reply
  • 1082 views

I do not know any scripting and wonder if anyone can help:

I need a button to calculate the sum of 5 "numbers(percentage and sometimes the number will be 0)" then ensure they equal 100% - if not a warning - then if it matches the 100% the button will take the user to a page (this part I am sure is easy).

Thank you.

I know that you can run javascript from button and go to page view, it is the javascript portion I am unsure of.

This topic has been closed for replies.
Correct answer Nesa Nurani

Try this, if sum is not 100 you will get alert else if sum = 100 it will take you to page 2:

var a = Number(this.getField("PA").valueAsString);
var b = Number(this.getField("PB").valueAsString);
var c = Number(this.getField("PC").valueAsString);
var d = Number(this.getField("PD").valueAsString);
var e = Number(this.getField("PE").valueAsString);

var total = a+b+c+d+e;

if(total != 100)
app.alert("Total must equal 100");
else
this.pageNum = 1;

1 reply

try67
Adobe Expert
January 20, 2023

Actually, you can't use the "Go to a page view" action for this, as it can't be connected to the JS one. It all has to be done in a single script, but it's quite easily done. What are the names of the fields you want to add? Also, what do you mean by some are numbers and some are percentages? Is the total always the same? If not, you can't add those up.

Liam1965Author
New Participant
January 22, 2023

On click >>>

Calculate "sum" of PA + PB + PC + PD + PE (Not all will have an amount)

"sum"   If  = 100 continue

"sum"   If  = greater or less than 100 then "error message" = Total must equal 100

Trust this explains it clearly - thank you for the help.

 

I  assume that if I then add an action after running javascript to "Go to page" it will work or if needed in Javascript will be go to page 2.

 

Thanks.

Nesa Nurani
Nesa NuraniCorrect answer
Adobe Expert
January 22, 2023

Try this, if sum is not 100 you will get alert else if sum = 100 it will take you to page 2:

var a = Number(this.getField("PA").valueAsString);
var b = Number(this.getField("PB").valueAsString);
var c = Number(this.getField("PC").valueAsString);
var d = Number(this.getField("PD").valueAsString);
var e = Number(this.getField("PE").valueAsString);

var total = a+b+c+d+e;

if(total != 100)
app.alert("Total must equal 100");
else
this.pageNum = 1;