Skip to main content
Participant
August 21, 2023
Answered

Field Displaying Percent of Form Completed

  • August 21, 2023
  • 2 replies
  • 793 views

New to Javascript, even newer to javascript in adobe. I have a fillable adobe form. It's been requested that I add a field that displays the percentage complete of the form. So as the user fills in more of the form, that number would change. Is there a formula I could use on a calculated text field for the default value? Something like, =this.fields.count / (count(this.fields <> "").

I realize I may have to have each field listed in the denominator as an this.field.x != ""...

This topic has been closed for replies.
Correct answer Thom Parker

Calculating the Percent completion of a form is tricky since not all fields need to be filled in, and some are calculated. If this is an issue on your form, then the %complete could be restricted to fields that are marked as required and can be filled by the user, i.e., skip fields that are marked as readOnly. 

 

But, just in general, the way to do this calculation is to compare entered field values to the fields default value.

 

var nSum = 0, nCnt = 0, oFld;

for(var i=0;i < this.numFields;i++)
{
   oFld = this.getField(this.getNthFieldName(i));
   // Skip buttons and readOnly fields, could also add required test here
   if(!oFld.readonly && (oFld.type != "button"))
   {
       nCnt++;
       if(oFld.value != oFld.defaultValue)
          nSum++;
   }
}
event.value = nSum/nCnt;

 

Formatting the output is done in the Format Script.

 

2 replies

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
August 21, 2023

Calculating the Percent completion of a form is tricky since not all fields need to be filled in, and some are calculated. If this is an issue on your form, then the %complete could be restricted to fields that are marked as required and can be filled by the user, i.e., skip fields that are marked as readOnly. 

 

But, just in general, the way to do this calculation is to compare entered field values to the fields default value.

 

var nSum = 0, nCnt = 0, oFld;

for(var i=0;i < this.numFields;i++)
{
   oFld = this.getField(this.getNthFieldName(i));
   // Skip buttons and readOnly fields, could also add required test here
   if(!oFld.readonly && (oFld.type != "button"))
   {
       nCnt++;
       if(oFld.value != oFld.defaultValue)
          nSum++;
   }
}
event.value = nSum/nCnt;

 

Formatting the output is done in the Format Script.

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
August 21, 2023

thank you so much! This works great. If I wanted to dispay this value as an integer would I cange the last line to
```
event.value = CInt(nSum/nCnt) * 100));```"

Participant
August 21, 2023

Well, swap the numerator and denominator....oops