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

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

Community Beginner ,
Sep 14, 2020 Sep 14, 2020

Copy link to clipboard

Copied

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

TOPICS
Acrobat SDK and JavaScript

Views

612

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 , Sep 14, 2020 Sep 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.

Votes

Translate

Translate
Community Expert ,
Sep 14, 2020 Sep 14, 2020

Copy link to clipboard

Copied

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

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 ,
Sep 14, 2020 Sep 14, 2020

Copy link to clipboard

Copied

LATEST

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.

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 ,
Sep 14, 2020 Sep 14, 2020

Copy link to clipboard

Copied

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.

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 ,
Sep 14, 2020 Sep 14, 2020

Copy link to clipboard

Copied

Awesome! That worked perfectly. 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