Skip to main content
Participating Frequently
May 3, 2021
Answered

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

  • May 3, 2021
  • 2 replies
  • 1237 views

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;

 

}

This topic has been closed for replies.
Correct answer BarlaeDC

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?

2 replies

JR Boulay
Community Expert
May 3, 2021

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

Acrobate du PDF, InDesigner et Photoshopographe
BarlaeDC
BarlaeDCCorrect answer
Community Expert
May 3, 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?

OayusufAuthor
Participating Frequently
May 3, 2021

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;

}