I have a multiline text field with a limited number of characters. I'd like to let people know how many characters they have typed thus far and the total number of characters permitted.
Does anyone know of a script that could be used to do that?
I'm using Acrobat X on Windows.
I use a function like the following to display in a separate field the remaining characters allowed in a field. The field name of this separate field is given by the f_name argument. This function is intended to be called in the Keystroke event of the text field, which also has a character limit set.
// Function in a document-level JavaScript
function remaining_count(f_name) {
// Get a reference to the field to display the remaining character count
var f = getField(f_name);
// Get the character limit of this text field
var max_chars = event.target.charLimit;
// Get a string of all the characters that have been entered into this field
var s = AFMergeChange(event);
// This many characters are in this field
var total_chars = s.length;
// Display the remaining character count
if (total_chars) {
f.value = "remaining: " + (max_chars - total_chars) + " chars";
} else {
f.value = ""; // Display nothing if this field is blank
}
}
Since you want to display the total characters entered instead of the number remaining, you could change the one line to something like:
f.value = total_chars + " characters out of " + max_chars;
You'd call this function like this:
// Custom Keystroke script for text field
remaining_count("char_count");
Where "char_count" is the name of a read-only text field that shows the character count.
George,
Thank you so much for your reply.
I'm such a noob at JavaScript, I must be missing something very simple.
This is what I've done:
There must be a place in the script to enter the name of the text field that has the character limit. It's name is txt.comment, but not sure how to do that.
My document JavaScript looks like this:
// Function in a document-level JavaScript
function remaining_count(f_txt_remainingCharacters) {
// Get a reference to the field to display the remaining character count
var f = getField(f_txt_remainingCharacters);
// Get the character limit of this text field
var max_chars = event.target.charLimit;
// Get a string of all the characters that have been entered into this field
var s = AFMergeChange(event);
// This many characters are in this field
var total_chars = s.length;
// Display the remaining character count
if (total_chars) {
f.value = "remaining: " + (max_chars - total_chars) + " chars";
} else {
f.value = ""; // Display nothing if this field is blank
}
}
By the way, what I actually want to show in the txt_remainingCharacters text field is the number of characters already typed and the total number of characters available. I'm sorry I didn't make that clear. So the display would look like this, for example:
255 characters allowed / 210 characters typed.
If the user tries to exceed the total number of characters, Acrobat beeps, but I'd like to give a little more specific message, such as displaying an error message:
"Maximum allowed characters exceeded."
The reason I'm trying to be so explicit is that this is an accessible form, and messages must be obvious to a blind person.
Did you also add the custom Keystroke script to the field that has the 255 character limit?
The script gets this limit with the following line:
// Get the character limit of this text field
var max_chars = event.target.charLimit;
To display what you want in the text field, the line of code would be:
f.value = max_chars + " characters allowed / " + total_chars + " characters typed.";
How exactly do you want to display the error message?
George,
I don't know where or how to add a custom Keystroke script to the field. Do I put it in Text Field Properties, Validate tab? Please give me some guidance on that.
As the person is typing, I would like the txt_remainingCharacters field to display something like:
230 characters typed / 255 characters allowed.
The first number (characters typed) should update and display the number of characters typed as the user continues typing. The second number, total number of characters allowed, would not change.
If the user tries to exceed the total number of characters (255 in this example), an alert text message should pop up and say something like "Maximum allowed characters exceeded."
No, that would be a custom Validate script. To enter a custom Keystroke script, go to the Format tab of the field properties dialog, select a format category fo Custom, and you will see where you can enter a custom Keystroke script. In your case, it should be:
remaining_count("ShowCharCount");
If you mean that you want to use this with several different fields, you would just have to set up separate fields to display the character counts and pass the field name when calling the function in the Keystroke script. For example:
// Custom keystroke script
remaining_count("ShowCharCount2");
Where "ShowCharCount2" is the field that will show the character count for your new text field.
North America
Europe, Middle East and Africa
Asia Pacific