5 Replies Latest reply on Aug 16, 2010 7:48 PM by WSpan01

    Acrobat Form - How to capitalize first letters of field words.

    WSpan01

      I have produced my first Acrobat Form and am setting validation rules on some of the fields.  I want to ensure all names and addresses are recorded with the first letter of each word being a capital and the remain letters being lower case.   While I have found and read quite a few postings, I have no scripting experience and the posts assume a reasonable understanding of scripting, debugging, etc.

       

      Would someone please explain how I may achieve this.

       

      Thanks

        • 1. Re: Acrobat Form - How to capitalize first letters of field words.
          try67 MVP & Adobe Community Professional

          Use this code as the custom validation script:

           

          if (event.value!=null && event.value!="") event.value =

          event.value.charAt(0).toUpperCase() + event.value.slice(1);

          1 person found this helpful
          • 2. Re: Acrobat Form - How to capitalize first letters of field words.
            WSpan01 Level 1

            Thanks for the tip.  It worked to capitalize the first letter of the first word in the sequence.  I need it to capitalize the first letter of every word in the sequence.  ??

            • 3. Re: Acrobat Form - How to capitalize first letters of field words.
              WSpan01 Level 1

              Thanks for the tip.  It worked to capitalize the first letter of the first word in the sequence.  I need it to capitalize the first letter of every word in the sequence.  ??

              • 4. Re: Acrobat Form - How to capitalize first letters of field words.
                gkaiseril MVP & Adobe Community Professional

                You need to fully understand how the data will be entered and what do you want to accomplish. If you want to have a function like Title Case, then you will have to adjust for articles or other words that would not be capitalized. Will the users use a different character other than a space for separating words?

                 

                Since this is a formatting action, I would place the script as the custom format or a validation script, so this code is not rerun each time a field used in a calculation is updated.

                 

                Assuming only a space character will separate the words, you can use the '.split()' method to make an array of words in the inputted text for a field. You can then loop through the array of words and setting the first character to upper case and the remainder of the word to lower case, using the 'substr()' method or other methods of the string property.

                 

                The simplest version of this could be:

                 

                var aInput = event.value.split(" "); // make an array split at space
                var sCharacter = '';
                var sWord='';

                 

                // for each element of word array, capitalize the first letter
                for(i = 0; i <aInput.length; i++) {
                      aInput[i] = aInput[i].substr(0, 1).toUpperCase() + aInput[i].substr(1) .toLowerCase();
                } // end loop for input string
                // rebuild input string with modified words with spaces
                event.value = aInput.join(' ');

                 

                But since you will be repeating this code in several fields, I would create some document level functions so the code will exist in one location and be pre-interpreted and run faster:

                 

                function UpperCase(sString) {
                return  sString.toUpperCase();
                }

                function LowerCase(sString) {
                return sString.toLowerCase();
                }

                function FirstUpperCase(sInput) {

                var aInput = sInput.split(" "); // make an array split at space
                var sCharacter = '';
                var sWord='';

                 

                // for each element of word array, capitalize the first letter
                for(i = 0; i <aInput.length; i++) {
                      aInput[i] = UpperCase(aInput[i].substr(0, 1) ) + LowerCase(aInput[i].substr(1) );
                } // end loop for input string
                // rebuild input string with modified words with spaces
                return aInput.join(' ');
                }

                 

                You can then use the following code in each field where you want to force the first character to upper case:

                event.value = FirstUpperCaseinputinputletter for(event.value);


                Another solution would be to use the following code in the format or validate script:

                 

                event.value = event.value.toLowerCase().replace(/\b\w/g, function(match){return match.toUpperCase();});

                This script uses the RegEx object and its '.replace()' method using a self defined funciton with the matched string.

                • 5. Re: Acrobat Form - How to capitalize first letters of field words.
                  WSpan01 Level 1

                  Thanks GKaiseril.  I appreciate your assistance.  I chose to set it at a document level.  It works perfectly.