Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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:
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;