Skip navigation
Currently Being Moderated

Display total number of characters allowed

Mar 28, 2012 11:04 PM

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.

 
Replies
  • George Johnson
    11,676 posts
    Aug 11, 2002
    Currently Being Moderated
    Mar 29, 2012 12:27 AM   in reply to jay fresno

    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.

     
    |
    Mark as:
  • George Johnson
    11,676 posts
    Aug 11, 2002
    Currently Being Moderated
    Mar 29, 2012 11:59 AM   in reply to jay fresno

    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?

     
    |
    Mark as:
  • George Johnson
    11,676 posts
    Aug 11, 2002
    Currently Being Moderated
    Mar 29, 2012 12:58 PM   in reply to jay fresno

    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");
    
     
    |
    Mark as:
  • George Johnson
    11,676 posts
    Aug 11, 2002
    Currently Being Moderated
    Apr 10, 2012 3:17 PM   in reply to jay fresno

    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.

     
    |
    Mark as:
  • George Johnson
    11,676 posts
    Aug 11, 2002
    Currently Being Moderated
    Apr 10, 2012 7:58 PM   in reply to jay fresno

    The function was designed to be used with any number of fields. It expects to be passed the name of a field to display the character count, and it expects that the fields that calls it has a character limit set.

     
    |
    Mark as:
  • George Johnson
    11,676 posts
    Aug 11, 2002
    Currently Being Moderated
    Apr 10, 2012 9:50 PM   in reply to jay fresno

    Yes indeed. Give it a try.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 14, 2013 2:47 PM   in reply to jay fresno

    You've mixed things up a little. This is what you want.

     

    The document JavaScript is:

    // Function in a document-level JavaScript

    function remaining_count(f_count) {

     

        // Get a reference to the field to display the remaining character count

        var f = getField(f_count);

     

        // 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 = max_chars + " characters allowed / " + total_chars + " characters typed.";

        } else {

            f.value = "";  // Display nothing if this field is blank

        }

     

    }

     

    The "count" field needs no keystroke or any other events. Just needs to be named "count".

     

    The "essay" field should have a Custom Keystroke Script as follows:

    remaining_count("count");

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points