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

Live counting of characters as a user types them in a field

skhalsa
Registered: Jun 4 2008
Posts: 21
Answered

I am building a form using Livecycle ES 8.2 and have a multi-line field that restricts the maximum number of characters entered (200). The tricky part is that we would like to have a live countdown of the number of characters the user has left available to them - as they are typing. I have managed to get it working to display the number, but the user has to leave the field for the number to update. Any ideas on how to make the countdown live?

Here's my code:
//TextField1 is the multi-line field and TextField1 is the read only field displaying the countdown.
//TextField2's calculate using FormCalc
var charCount = Len(TextField1[*]);
var totalCount = 200;
xfa.event.change = totalCount - charCount;

Thanks, Siri

My Product Information:
LiveCycle Designer, Windows
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
1. Your syntax for FormCalc is not correct!

var charCount = Len(TextField1[*]);
var totalCount = 200;
xfa.event.change = totalCount - charCount;

FormCalc doesn't use the ; at the end of the code lines!


2. A shorter way to calculate the numbers of chars is

200 - Len(TextField1) //put this into the change event of the field that shows the available chars


3. But the reason, why it also does not work in realtime is...

change

This event fires when the content of the field is changed by the user. This event fires on every
keystroke as long as the field has keyboard focus. It also fires when the user pastes into the field,
makes a selection from a choice list or drop-down menu, checks or unchecks a checkbox, or
changes the setting of a set of radio buttons. [b]It is not fired by content changes that are made by
the XFA application, for example calculations, nor is it fired by a merge operation.[/b]

Maybe, that you can solve this with a counter script.
Thom Parker relesed some examples for this in the past.
But using a timer for calculating the users input live may cause the form becomes rapidly slow.

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

skhalsa
Registered: Jun 4 2008
Posts: 21
Thanks for replying - the simpler code it doesn't work for me when I put it in the change event of the countdown display field, though:

200 - Len(TextField1) //put this into the change event of the field that shows the available chars

For now I'll use the lengthier code (sans semi-colons :) since it seems to work.

Do you know where Thom Parker's examples are located for a counter script? I think it's worth testing to see how slow the form gets.

Thanks, Siri
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
http://www.acrobatusers.com/tutorials/working-date-and-time-acrobat-javascript-part-3-3

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

formman
Registered: Jul 17 2008
Posts: 44
Here is what I did to create a counter:

Create a text field, set the limit to 200 characters.
Create a numeric field.
In the change event of the text field enter the following:

//JavaScript
var a = xfa.event.newText.length;
NumericField1.rawValue = 200 - a; //NumericField1 is the default name of the numeric field

Rick Kuhlmann
Tech-Pro

I tried this in LCD 8.1 and Reader 9.0 in Print Preview and it seems to work pretty well.

Forms Developer/Designer
Tech-Pro, Inc.
Roseville, MN

radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
@formman

Really nice solution for the problem. Thumbs up! =)

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

skhalsa
Registered: Jun 4 2008
Posts: 21
Agreed! I tested it with a 7.0.5 target and it works perfectly. Thanks for your post, Siri