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

Community Beginner ,
Apr 11, 2020 Apr 11, 2020

Copy link to clipboard

Copied

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.

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

144

Likes

translate

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

correct answers 1 Correct Answer

Adobe Community Professional , Apr 11, 2020 Apr 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?  

Likes

translate

Translate

Translate
Adobe Community Professional ,
Apr 11, 2020 Apr 11, 2020

Copy link to clipboard

Copied

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

Likes

translate

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 Beginner ,
Apr 11, 2020 Apr 11, 2020

Copy link to clipboard

Copied

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

Likes

translate

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 Beginner ,
Apr 11, 2020 Apr 11, 2020

Copy link to clipboard

Copied

Can I use this code multiple times for different text fields on the form? Or does it need to be individual for each field besides changing "Text 1" to "Text 2"?

Likes

translate

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
Most Valuable Participant ,
Apr 12, 2020 Apr 12, 2020

Copy link to clipboard

Copied

You can, and should, put it in a function as a doc-level script and provide it with the name of the field as a parameter.

Likes

translate

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
Adobe Community Professional ,
Apr 12, 2020 Apr 12, 2020

Copy link to clipboard

Copied

You can copy the code into any other text field keystroke event, the only needed change is the output field name, as you've indicated.

 

As Try67 mentions, the best methodology for repeated use is to abstract the functionality into a document level function. Then call the function from the keystroke scripts. This is particularly helpful if it is used many times. 

 

Here's an example, notice that the input to this function is the name of the field where the result is displayed. 

 

 function MyCountChars(strRsltFld)
   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(strRsltFld).value = strFull.length
    }
}

 

 

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

Likes

translate

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 Beginner ,
Apr 12, 2020 Apr 12, 2020

Copy link to clipboard

Copied

I guess I'm not understanding how I would call that script multiple times across multiple text fields so each one displayed the count of the field I am currently typing in. How would I correctly call each individual text field in their respective keystrokes?

Likes

translate

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
Most Valuable Participant ,
Apr 12, 2020 Apr 12, 2020

Copy link to clipboard

Copied

Using this example provided above, this is the code you would place under the Keystroke event of the first field:

 

MyCountChars("Text1");

Likes

translate

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 Beginner ,
Apr 12, 2020 Apr 12, 2020

Copy link to clipboard

Copied

Thank you both for your help!

Likes

translate

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 Beginner ,
Apr 12, 2020 Apr 12, 2020

Copy link to clipboard

Copied

I got all my fields updated and put the suggested code into my document level script, but the first field is the only one that seems to run the script. The other fields don't show the character count while typing, only when clicking out of the field.

I've attached a link to the file if that is at all helpful.

 

PDF Link 

Likes

translate

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
Adobe Community Professional ,
Apr 12, 2020 Apr 12, 2020

Copy link to clipboard

Copied

But you are wrong, the script is working perfectly and the text fields are updating. You just can't see it because the count result field is behind the field the user is typing into, so the display of the result is frozen in time. Move it so it is not being blocked by the main field.

 

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

Likes

translate

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 Beginner ,
Apr 13, 2020 Apr 13, 2020

Copy link to clipboard

Copied

Oh goodness. That did fix it. Thank you so much for all the help!

Likes

translate

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