• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Calculate on button click plus verify total

Community Beginner ,
Jan 20, 2023 Jan 20, 2023

Copy link to clipboard

Copied

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.

TOPICS
How to , JavaScript , PDF forms

Views

514

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 Expert , Jan 22, 2023 Jan 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;

Votes

Translate

Translate
Community Expert ,
Jan 20, 2023 Jan 20, 2023

Copy link to clipboard

Copied

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.

Votes

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 ,
Jan 21, 2023 Jan 21, 2023

Copy link to clipboard

Copied

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.

Votes

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 Expert ,
Jan 22, 2023 Jan 22, 2023

Copy link to clipboard

Copied

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;

Votes

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 ,
Jan 22, 2023 Jan 22, 2023

Copy link to clipboard

Copied

LATEST

Thank you Nesa, appreciate the help. Works exactly as needed.

Have a lovely day.

Votes

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