Skip to main content
Known Participant
May 4, 2022
Answered

Adobe Form Custom Validation Adding to 100%

  • May 4, 2022
  • 2 replies
  • 2739 views

Hello,

I have a form where users are asked to enter 5 numbers as percentages, where they are all supposed to total 100%. 

I need to have an error pop up if the total does not equal 100%. I have found scripts that can do this however, if the script is for under or equals 100, there is a pop up after every number is entered which isn't ideal. 


Is there a way to do this validation that only provides an error if the total is under or over 100% but only once all the 5 fields have been filled? (i.e., once/if none of the fields are zero) 

 

Please let me know!  Thanks. 

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

You didn't provide field names, so I use "Text1"-"Text6" ( make sure your fields are named in sequence)

as custom calculation script of total field use this:

var sum = 0;
var check = 0;
for(var i=1; i<=6; i++){
if(this.getField("Text"+i).valueAsString != ""){
check++;
sum += Number(this.getField("Text"+i).value);}}
if(check == 6 && sum != 100)
app.alert("enter message here");
event.value = sum;

2 replies

Nesa Nurani
Community Expert
May 4, 2022

You said user enters 5 numbers, in your screenshot there are 6 percentage fields+total percentage fields?

What are names of the fields?

 

Known Participant
May 4, 2022

Apologies - they complete 6 lines which are totalled. I've attached a clearer screenshot 

Nesa Nurani
Nesa NuraniCorrect answer
Community Expert
May 4, 2022

You didn't provide field names, so I use "Text1"-"Text6" ( make sure your fields are named in sequence)

as custom calculation script of total field use this:

var sum = 0;
var check = 0;
for(var i=1; i<=6; i++){
if(this.getField("Text"+i).valueAsString != ""){
check++;
sum += Number(this.getField("Text"+i).value);}}
if(check == 6 && sum != 100)
app.alert("enter message here");
event.value = sum;

Bernd Alheit
Community Expert
May 4, 2022

Is a value of 0 allowed in the fields?

Known Participant
May 4, 2022

No - all fields must have a number over zero

Bernd Alheit
Community Expert
May 4, 2022

Display the popup only when all values are greater as zero.