Skip to main content
Participant
March 27, 2024
Answered

Java script needed to count form fields with text and ignore empty fields for Acrobat Pro

  • March 27, 2024
  • 1 reply
  • 883 views

What is the Java script that is needed for box4 to reflect the count of the number of x's in the form fields box1, box2, box3 disregarding any the blank form fields? In this sample, the form fields are labeled box1, box2, box3, and box4 left to right.  

 

I have found the java script below but do not know where to substitute box1, box2, box3, and box4 or what will need to removed from this java script.

 

 

var total = "";

for (var i=1; i<=28; i++) {var f = Number(this.getField("Rating"+i).valueAsString);

if (f > 0 && f <= 4) total++;} event.value = total;

 

Thank you for your assistance!!

Janelle

This topic has been closed for replies.
Correct answer Janelle354870978x5k

Would it be correct to use

var total = 0; for(var i=5; i<=7; i++) { var f = this.getField("box"+i).valueAsString; if(f === "X") total++;} event.value = (total == 0) ? "" : total;

 

 

 

1 reply

Nesa Nurani
Community Expert
Community Expert
March 27, 2024

If the value is exactly "X" use this:

 

var total = 0;
for(var i=1; i<=4; i++) {
var f = this.getField("box"+i).valueAsString;
if(f === "X") total++;} 
event.value = (total == 0) ? "" : total;

 

Participant
March 27, 2024

Thank you! This script works well for a single row. Apparently, I have a lot to learn about Java script.

 

What would I do if I have a second row of boxes labeled box5, box6, box7 and box8 that require their own calculation of X to be shown in box8? Would I place two different Java scripts in box4 and box8's properties?

 

 

Janelle354870978x5kAuthorCorrect answer
Participant
March 27, 2024

Would it be correct to use

var total = 0; for(var i=5; i<=7; i++) { var f = this.getField("box"+i).valueAsString; if(f === "X") total++;} event.value = (total == 0) ? "" : total;