Skip to main content
Inspiring
February 17, 2020
Question

Word counter for text field in Acrobat DC form

  • February 17, 2020
  • 1 reply
  • 5752 views

Hi, I've been googling all over, and am having a heck of a time making this work.

I have a text field in an Acrobat form that needs to let the user know how many words they have entered.

My field to be counted is called "Statement" and the word counter field is called "WordCount'.

I was able to adat the follow javascript, which I put as a Custom Keystoke Script for "Statement":

 

var aWords = event.value.split(" ");
this.getField("WordCount").value =
aWords.length;

 

 and it sort of works, however the WordCounter field does not get updated correctly.... it lags behind one or two words, and if a word is deleted and retyped, the counter gets behind even more. Even if the "Statement" is cleared completely (not reset, but just using the delete key), WordCounter shows 1.

It's so close... I just need WordCounter to be more accurate.

What am I doing wrong?

Any help would be greatly appreciated.

 

BTW, a lot of older discussions on the forum keep referring to https://forums.adobe.com/thread/1094129 which is a dead link (it just goes back to the main community page.) Any way of resurrecting that discussion? 

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
February 17, 2020

You're close, but the event.value in the keystroke script is not what you think, it's the value of field before the user types, the current user entry is in "event.change". If you want the counter to update as the user is typing, then you have to assemble the complete text. 

 

You'll find and example of this in the Acrobat JavaScript reference, here:

https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/#t=Acro12_MasterBook%2FJS_API_AcroJS%2Fevent_properties.htm%23TOC_selEndbc-13&rhtocid=_6_1_8_27_4_12

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
akunceAuthor
Inspiring
February 17, 2020

I'm not good enough with javascript to figure out how to implement the event.change. And I'm not following the link you provided... it's beyond me.

Thanks, anyway.

Thom Parker
Community Expert
Community Expert
February 18, 2020

A simpler solution is to use your first script in the Validate event for the field. Then, when the user clicks out of the field (or hits return)  the validate script will update the word count. 

 

BTW, here's a better word count script:

 

var aWords = event.value.replace(/(^\s+)|(\s+$)/g,"").split(/\s+/mg).length;
this.getField("WordCount").value = aWords.length;
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often