Skip to main content
Inspiring
Feb 12, 2019 2:56 PM UTC
Answered

Sum percentage fields and validate against 100%

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?

I appreciate your help.

This topic is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Feb 12, 2019 3:45 PM UTC

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.


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...

13 replies

Participant
Jun 25, 2020 8:07 PM UTC

@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?

Translate
try67
Community Expert
Community Expert
Jun 25, 2020 8:38 PM UTC

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)

Translate
Participant
Jun 25, 2020 8:58 PM UTC

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. 

Translate
try67
Community Expert
Community Expert
Feb 12, 2019 3:29 PM UTC

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...

Translate
Inspiring
Feb 12, 2019 3:37 PM UTC

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?

Translate
try67
Community Expert
Community Expert
Feb 12, 2019 3:38 PM UTC

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...

Translate