These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Dynamic Character Counter PDF form

Guzzy156
Registered: Jun 30 2010
Posts: 9
Answered

Hi All,

Hoping someone is able to help with this - I'm trying to do a character count where the number in a text field changes as the user types into another text field....

I have a text field ("count") which has the value of 255 then another text field ("reason") which is limited to 255 characters...

In the ("count") text field properties I have a javascript action which has this code:

getField("count").value = 255;

myString = new String (getField("reason").valueAsString);
var stringLen = myString.length // assigns length of string to variable stringLen

getField("count").value = 255 - stringLen; // a.value - stringLen;

which works fine, but the number in the ("count") text field only updates once the ("reason") text field has changed focus to another text field... is it possible to have the number in ("count") text field update "dynamically" as the user types in the ("reason") text field?

Thanks in Advance

Guzzy156

My Product Information:
Acrobat Standard 7.0.1, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
To update the count field as the user types, you need to use a custom Keystroke script for the "reason" field. Set up a document-level function like the that shown below and call it from the custom Keystroke script of the "reason" field. For example:

// This is the custom Keystroke script
char_count("count");

// This is the document-level function
function char_count(f_name) {

if (!event.willCommit) {
getField(f_name).value = 255 - AFMergeChange(event).length;
}
}

Instead of hardcoding the character limit (255), you may want to set the character limit for the field and get that value in the script, making the script more generic.

e.g.,

getField(f_name).value = event.target.charLimit - AFMergeChange(event).length;


Then you might want to consider what should happen if the fields get reset or the value of "reason" is set programmatically or by importing a data file.

BTW, do not place this function in the Keystroke script.
Guzzy156
Registered: Jun 30 2010
Posts: 9
Thanks for the quick reply George will give it a try when I'm back at work on Monday - was looking at the keystroke actionset in the Adobe Acrobat Jscript pdf, but wasn't sure how to construct it... Thanks for your help...

Guzzy156

Guzzy156
Registered: Jun 30 2010
Posts: 9
Huge Thanks George - your function worked a treat, didn't realise there was a Custom Keystroke Script under the Format tab until I'd searched the AcrobatUsers site....

Cheers

Guzzy156

43rdworld
Registered: Dec 19 2010
Posts: 2
I just ran across this post and it's just what I need - almost. I get the scripts working just fine but I'm using a multi-line text box but when I paste the script into the custom keystroke script, I'm forced into single line mode in the textbox.

Any idea how to work around that to get multi-line back?
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
43rdworld,

What version of Acrobat are you using?
43rdworld
Registered: Dec 19 2010
Posts: 2
I'm using 8.25. In the interim, I've found that setting the appropriate field name to "multiline = true" can work but it's a case of rubbing your stomach and patting your head while standing on one leg. If the field is blank and I paste in the script, multiline won't work. If the field has 2 or more lines of text in it before I edit the field properties to add the script, multiline=true will work. If I have to re-open the field for any reason, the field will revert to single line and I have to start all over.

If this is the way it has to be done that's fine. But if you have a better solution, that would be great.

For better or worse, I modified your script as follows - I have multiple counters on multiple fields so I opted for the original version and have hard coded the relevant field names.

// This is the custom Keystroke script
char_count("suCount");
this.getField("serviceUnit").multiline=true;
// This is the document-level function
function char_count(f_name) {

if (!event.willCommit) {
getField(f_name).value = ("(") + (250 - AFMergeChange(event).length) + (" Characters Remaining)");
}
}