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

Sum percentage fields and validate against 100%

Explorer ,
Feb 12, 2019 Feb 12, 2019

Copy link to clipboard

Copied

Hello;

I have a form where the client is asking that 8 numeric fields (labeled as percentages) are totaled and validated against 100%. If the total is over 100, they have one alert message, and if it's over 100, they have another alert message. This alert also needs to let the user know (I guess print out within the alert message) what their total is so they can make the necessary corrections to equal 100. Is this possible?

screenshot_sumpercentages.png

I appreciate your help.

TOPICS
Acrobat SDK and JavaScript , Windows

Views

2.0K

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 , Feb 12, 2019 Feb 12, 2019

As I said, create a (hidden) text field that calculates the total, using the built-in Sum function under the Calculate tab, and use the following as its custom validation script:

if (event.value>100) app.alert("The maximum allowed value is 100%. Your total is : " + event.value + "%. Please fix it.",1);

Or something like that...

Votes

Translate

Translate
Community Expert ,
Feb 12, 2019 Feb 12, 2019

Copy link to clipboard

Copied

You wrote "over 100%" twice... Did you mean one alert if it's over 100, and one if it's under?

If so, I would recommend you re-think the latter, at least, as it will cause an alert message to appear while the user is still filling in the values, which can be very annoying...

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
Explorer ,
Feb 12, 2019 Feb 12, 2019

Copy link to clipboard

Copied

Sorry, yes. A typo. Should be under and over 100%.

I thought having two error messages was a bit much as well. Maybe I can talk them into one error message when the sum of the fields do not equal 100, and provide what the current number is so they can make the corrections. Would that be better?

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 ,
Feb 12, 2019 Feb 12, 2019

Copy link to clipboard

Copied

I think so, yes. I would use a (hidden) total field to do it, although that will mean the error message will appear any time any field is changed, until it's fixed...

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
Explorer ,
Feb 12, 2019 Feb 12, 2019

Copy link to clipboard

Copied

What would be a best practice for something like this? This little section is only on page 1 of a 12 page form with a lot of fields in it. I guess we should only validate if the total goes over 100. Would like for it to be fairly simple for the user, if possible.

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 ,
Feb 12, 2019 Feb 12, 2019

Copy link to clipboard

Copied

As I said, create a (hidden) text field that calculates the total, using the built-in Sum function under the Calculate tab, and use the following as its custom validation script:

if (event.value>100) app.alert("The maximum allowed value is 100%. Your total is : " + event.value + "%. Please fix it.",1);

Or something like that...

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
Explorer ,
Feb 12, 2019 Feb 12, 2019

Copy link to clipboard

Copied

Worked perfectly! Thank you.

I even figured out how to do the "less than 100%" that we talked about. We'll see if my client still wants this functionality when they test it.

Thanks again!!

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
New Here ,
Jun 25, 2020 Jun 25, 2020

Copy link to clipboard

Copied

@try67's suggested script works pefectly for >100, but how would you alter that script to also throw an alert for a sum that is <100?

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 ,
Jun 25, 2020 Jun 25, 2020

Copy link to clipboard

Copied

If you do that the user will constantly get error messages while filling in the various fields, which can be very annoying...

If you do want to do it just change this part of the code:

(event.value>100)

To:

(event.value!=100)

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
New Here ,
Jun 25, 2020 Jun 25, 2020

Copy link to clipboard

Copied

I agree that is annoying for the user.

 

Is there no way that you know of to script both alerts (for >100 and <100) without throwing the alert on each field entry? 

 

Thanks, in advance. 

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 ,
Jun 25, 2020 Jun 25, 2020

Copy link to clipboard

Copied

I can think of two possible workarounds:

- Only show the alert when all the fields are filled-in AND the total is not 100.

- Show a written message on the page (using a read-only text field) instead of an alert window.

The two can be combined, of course.

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 ,
Feb 18, 2024 Feb 18, 2024

Copy link to clipboard

Copied

Hi Try67

 

How do you script for only show alert when all fields are filled in AND the total is not 100 please?

 

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 ,
Feb 19, 2024 Feb 19, 2024

Copy link to clipboard

Copied

Let's say the fields are called Amount1 to 3, and there's a Total Alert text field with the (read-only) text message you want to display (make sure to set it as the field's default value, so it doesn't go away when the form is cleared). You can use the following code as the custom calculation script of the latter:

 

var allFilled = true;
var total = 0;
for (var i=1; i<=3; i++) {
	var f = this.getField("Amount"+i);
	var v = f.valueAsString;
	if (v=="") allFilled = false;
	else total+=Number(v);
}
if (allFilled && total!=100) {
	event.target.display = display.visible;
} else event.target.display = display.hidden;

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 ,
Feb 19, 2024 Feb 19, 2024

Copy link to clipboard

Copied

LATEST

Thank you so much!! 

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