Script to count filled fields in adobe acrobat form

Contributor ,
Mar 13, 2018

Copy link to clipboard

Copied

I am trying to make a form in which it is required to count the number of filled fields .

The fillable fields are named as FIELD1, FIELD2, FIELD3 ..... ........................FIELD 27, FIELD 28.

Below is the javascript i am using to count the filled fields.

var counter = 0;

for (i=1; i<=28; i++) {

    if (this.getField("FIELD" + i).value!=="") counter++;

}

if (counter>0) {

    event.value = counter;

} else event.value = "";

Everything is working fine.. except for that it also counts the fields in which spaces are put......... which i don't want.. i want if only texts or numbers are input into field then only count the fields.. .

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

Change this line:

if (this.getField("FIELD" + i).value!=="") counter++;

To:

if (/^\s*$/.test(this.getField("FIELD" + i).valueAsString)==false) counter++;

TOPICS
Acrobat SDK and JavaScript, Windows

Views

520

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

Script to count filled fields in adobe acrobat form

Contributor ,
Mar 13, 2018

Copy link to clipboard

Copied

I am trying to make a form in which it is required to count the number of filled fields .

The fillable fields are named as FIELD1, FIELD2, FIELD3 ..... ........................FIELD 27, FIELD 28.

Below is the javascript i am using to count the filled fields.

var counter = 0;

for (i=1; i<=28; i++) {

    if (this.getField("FIELD" + i).value!=="") counter++;

}

if (counter>0) {

    event.value = counter;

} else event.value = "";

Everything is working fine.. except for that it also counts the fields in which spaces are put......... which i don't want.. i want if only texts or numbers are input into field then only count the fields.. .

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

Change this line:

if (this.getField("FIELD" + i).value!=="") counter++;

To:

if (/^\s*$/.test(this.getField("FIELD" + i).valueAsString)==false) counter++;

TOPICS
Acrobat SDK and JavaScript, Windows

Views

521

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
Mar 13, 2018 0
Most Valuable Participant ,
Mar 13, 2018

Copy link to clipboard

Copied

Change this line:

if (this.getField("FIELD" + i).value!=="") counter++;

To:

if (/^\s*$/.test(this.getField("FIELD" + i).valueAsString)==false) counter++;

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
Reply
Loading...
Mar 13, 2018 0
Contributor ,
Mar 13, 2018

Copy link to clipboard

Copied

Thanks you very much that works great......

but what about if i want to count only the numerical value not the string value or vice versa  .

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
Reply
Loading...
Mar 13, 2018 0
try67 LATEST
Most Valuable Participant ,
Mar 13, 2018

Copy link to clipboard

Copied

Then you would need to define what counts as a "numerical value" and what doesn't.

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
Reply
Loading...
Mar 13, 2018 0