JavaScript code that allows me to count the numbers of text fields that have contents in them.

Community Beginner ,
May 03, 2021 May 03, 2021

Copy link to clipboard

Copied

Hello, I am trying to find a JavaScript code that allows me to count the numbers of text fields that have contents in them.

 

I was able to do it for check boxes using the following code:

Var sum = 0;

for (var i = 1; i < 70; i +=) {

if (getField("V" + i).value == "Yes") {sum += 1}

 

event.value = sum;

 

}

TOPICS
How to, JavaScript, PDF forms

Views

337

Likes

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

Adobe Community Professional , May 03, 2021 May 03, 2021
Hi, Assuming the naming is something similar that enables you to loop through them then you can check the contents by calling: if (this.getField("T" + i).value != "") { sumText += 1} i have assumed the text fields are call T1, T2, T3, etc. respond to this if they are not?

Likes

Translate

Translate
Adobe Community Professional ,
May 03, 2021 May 03, 2021

Copy link to clipboard

Copied

Hi,

 

Assuming the naming is something similar that enables you to loop through them then you can check the contents by calling:

if (this.getField("T" + i).value != "") { sumText += 1}

 

i have assumed the text fields are call T1, T2, T3, etc. respond to this if they are not?

Likes

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 ,
May 03, 2021 May 03, 2021

Copy link to clipboard

Copied

Thank you so much! Your answer helped me solve the issue. Below is the final look of the script that finally worked.

var sum = 0;
var sumText = 0; 
for (var i = 1; i < 150; i += 1) {

    if (getField("V" + i).value == "Yes") {sum += 1}
    if (this.getField("A" + i).value != "") {sumText += 1}

event.value = sum/sumText*100;

}

 

Likes

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 ,
May 06, 2021 May 06, 2021

Copy link to clipboard

Copied

LATEST

Hi, is there a way for me to manipulate the script and ensure this field remains blank if  ("A" + i). value is blank? I keep getting this error message when all the fields are empty. Thanks. 

Screenshot 2021-05-04 134047.jpg

Likes

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
Adobe Community Professional ,
May 03, 2021 May 03, 2021

Copy link to clipboard

Copied

You can use this script:

 

var total = 0;
for (var i = 0; i < this.numFields; i++) {
var oFld = this.getField(this.getNthFieldName(i));
if ((oFld.type == "text") && (oFld.value != "")) {
total = total +1;
}}
console.clear();
console.show();
console.println("Total: " +total);

Likes

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