15 Replies Latest reply: Aug 16, 2013 7:32 AM by jay fresno RSS

    Display total number of characters allowed

    jay fresno

      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.

        • 1. Re: Display total number of characters allowed
          George_Johnson CommunityMVP

          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.

          • 2. Re: Display total number of characters allowed
            jay fresno Community Member

            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:

            • Copied your scipt into a document JavaScript, which I named ShowCharCount.
            • Replaced the f_name with the name of the text field that is used to display the character limit. It's name is txt_remainingCharacters. So the replacement is f_txt_remainingCharacters.

            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.

            • 3. Re: Display total number of characters allowed
              George_Johnson CommunityMVP

              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?

              • 4. Re: Display total number of characters allowed
                jay fresno Community Member

                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."

                • 5. Re: Display total number of characters allowed
                  George_Johnson CommunityMVP

                  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");
                  
                  • 6. Re: Display total number of characters allowed
                    jay fresno Community Member

                    George,

                    Got it! Everything is now working perfectly. Thanks so much for your help.

                    • 7. Re: Display total number of characters allowed
                      jay fresno Community Member

                      George,

                      I have one more related question. If there is more than one form field that has a character count limit, how would multiple fields be indicated in the document level JavaScript?

                      • 8. Re: Display total number of characters allowed
                        George_Johnson CommunityMVP

                        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.

                        • 9. Re: Display total number of characters allowed
                          jay fresno Community Member

                          Would any change need to be made to the Document JavaScript?

                          • 10. Re: Display total number of characters allowed
                            George_Johnson CommunityMVP

                            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.

                            • 11. Re: Display total number of characters allowed
                              jay fresno Community Member

                              Thanks, George. I assume from your response that no change needs to be made to the document JavaScript, and that (f_name)in your document JavaScript is generic and can be used for any number of fields in the PDF. Is that the case?

                              • 12. Re: Display total number of characters allowed
                                George_Johnson CommunityMVP

                                Yes indeed. Give it a try.

                                • 13. Re: Display total number of characters allowed
                                  jay fresno Community Member

                                  George,

                                  I'm using Acrobat XI now, and am revisitng this because have another form that needs to display the number of characters remaining in a field. I've tried over and over again to make the script work, but have not been successful.

                                   

                                  I would so much appreciate if you could explain what I'm doing wrong.

                                   

                                  I have a field named "essay" which has a maximum character count specified in Text Field Properties, in the Options tab.

                                  I have another text field named "count" where I want to display the remaining number of characters that can be typed.

                                   

                                  The "count" field has the following custom keystroke script:

                                  remaining_count("ShowCharCount");

                                   

                                  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 = "remaining: " + (max_chars - total_chars) + " chars";

                                      } else {

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

                                      }

                                    

                                  }

                                   

                                  The "essay" field has a Custom Keystroke Script as follows:

                                  // Get the character limit of this text field

                                      var max_chars = event.target.charLimit;

                                   

                                         f.value = max_chars + " characters allowed / " + total_chars + " characters typed.";

                                  • 14. Re: Display total number of characters allowed
                                    KCraig530

                                    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");

                                    • 15. Re: Display total number of characters allowed
                                      jay fresno Community Member

                                      kCraig,

                                      Thank you for so clearly explaining what needs to be done.