Skip to main content
Participating Frequently
Apr 11, 2020 8:53 PM UTC
Answered

Creating a character counter with a default starting value of 1 not 0

I'm trying to create a visible character counter that has a default start of 1. Currently, the first letter I type displays as 0. Here is the script I have in my keystrokes:

 

if(!event.willCommit)
{
var nChars = event.selEnd - event.selStart;

var aFull = event.value.split("");

aFull.splice(event.selStart, nChars, event.change);

var strFull = aFull.join("");

var aWrds = strFull.split(/\s+/);\

this.getField("Text1").value=event.value.split("").length

}

 

I'd also like the counter to revert to 0 when the field is empty.

This topic is closed to new replies. Start a new post to keep the conversation going.
Thom Parker
Thom ParkerCorrect answer
Community Expert
Apr 11, 2020 9:39 PM UTC

The text field is displaying the value of the field before the keystroke. If you want to include the keystroke, then use strFull.

You also don't need to split a string to get it's length. This is a standard string property

 

this.getField("Text1").value = strFull.length;

 

Where did you find this code?  

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

11 replies

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
Apr 11, 2020 9:39 PM UTC

The text field is displaying the value of the field before the keystroke. If you want to include the keystroke, then use strFull.

You also don't need to split a string to get it's length. This is a standard string property

 

this.getField("Text1").value = strFull.length;

 

Where did you find this code?  

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
Apr 11, 2020 11:06 PM UTC

It's patched together from a few different forum posts I found on here. Older ones. That worked perfectly, thank you.

Translate