• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Word counter for text field in Acrobat DC form

Explorer ,
Feb 17, 2020 Feb 17, 2020

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? 

TOPICS
How to , PDF forms

Views

2.0K

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
community guidelines
Community Expert ,
Feb 17, 2020 Feb 17, 2020

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:

https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/#t=Acro12_MasterBook%2FJS_API_Acro...

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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
community guidelines
Explorer ,
Feb 17, 2020 Feb 17, 2020

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.

Votes

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
community guidelines
Community Expert ,
Feb 18, 2020 Feb 18, 2020

Copy link to clipboard

Copied

LATEST

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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
community guidelines