Skip to main content
Greenprints Clean NRG
Known Participant
September 14, 2020
Answered

Count Number of fields IF field is filled-in vs blank

  • September 14, 2020
  • 2 replies
  • 1333 views

Hi! I am looking for code that I can put in the "Custom Calculation Script" section of the Text Field Properties in Acrobat Pro DC.

 

Part of the form I am creating has six fields:

String 1 - No.

String 2 - No.

String 3 - No.

Total Strings

String 4 - No.

String 5 - No.

String 6 - No.

Total Strings

 

I need to know how many fields get used, not the sum total of what's in the fields. For instance, if the user types:

String 1 - No. = 8

String 2 - No. = 10

String 3 - No.

String 4 - No. = 12

String 5 - No.

String 6 - No.

I need to know how many the first three strings are used (2) and how many from the second three strings are used (1).

String 1 - No. = 8

String 2 - No. = 10

String 3 - No.

Total Strings = 2

String 4 - No. = 12

String 5 - No.

String 6 - No.

Total Strings = 1

This topic has been closed for replies.
Correct answer try67

You can use something like this for the first group, as the custom calculation script of the total field:

var count = 0;
for (var i=1; i<=3; i++) {
	var v = this.getField("String "+i+" - No.").valueAsString;
	if (v!="") count++;
}
event.value = count;

And then just change the starting and ending numbers in the loop for the second group.

2 replies

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
September 14, 2020

You can use something like this for the first group, as the custom calculation script of the total field:

var count = 0;
for (var i=1; i<=3; i++) {
	var v = this.getField("String "+i+" - No.").valueAsString;
	if (v!="") count++;
}
event.value = count;

And then just change the starting and ending numbers in the loop for the second group.

Greenprints Clean NRG
Known Participant
September 14, 2020

Awesome! That worked perfectly. Thank you so much. 🙂

ls_rbls
Community Expert
Community Expert
September 14, 2020

Do you already have a code that you've worked on and can share here ?

 

This may be possible using an array method to count how many  text fields  are not empty (or null, or blank if you prefer). 

Greenprints Clean NRG
Known Participant
September 14, 2020

I have pretty much no code experience. I just read through and try to guess what it means and fiddle with it until I get what I want. So, I was just using code I found in other parts of the community, but I couldn't get anything to work.