Skip to main content
Participating Frequently
April 11, 2020
Answered

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

  • April 11, 2020
  • 1 reply
  • 1473 views

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 has been closed for replies.
Correct answer Thom Parker

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?  

1 reply

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
April 11, 2020

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
Participating Frequently
April 11, 2020

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