12 Replies Latest reply on Feb 19, 2018 4:15 PM by JeffBPaarsa

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

          2 people 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.

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

                    Hi,

                     

                    This only upper case the first word pf the string...  say "this was a test"  will be changed to "This was a test".

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

                      Hi,

                       

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

                       

                      The above code gave me syntax error when I added under the validate tab of the field... FirstUpperCaseinputinputletter?

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

                        You need to add the code for the functions to your form's document level JavaScripts, since it is not part of Acrobat but added to the form.

                        • 9. Re: Acrobat Form - How to capitalize first letters of field words.
                          Thom Parker Adobe Community Professional

                          Here's a slightly shorter version of the same functionality using a regular expression replace. You can't underestimate the power of regular expressions

                           

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

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

                            Hi,

                             

                            Thank you, I used the regular expression code and that worked fine, since I am doing that routine a lot on some of my forms, I was thinking rather than repeating the same code over and over Just put them in JavaScript and call the routine for each fields I need to run that code.

                             

                            I just could not figure out where I need to put this line of code that does not give me the syntax error.

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

                             

                            Regards,

                            Jeff P.

                            • 11. Re: Acrobat Form - How to capitalize first letters of field words.
                              Thom Parker Adobe Community Professional

                              First, I see that G already supplied you with the shorter solution. That's the code that should be put in the function. G was just trying to explain how it's done be breaking the code into parts.

                               

                              Now, one of the problems with your line of code is that there is a space after the function name, followed by the word "for"?  There are too many things wrong with this to explain, but its just wrong.

                              The next issue is that there is no function called "FirstUpperCaseinputinputletter". I think G was just typing too fast.

                              Use this code instead

                               

                              event.value = FirstUpperCase(event.value);

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

                                Hi,

                                 

                                Yes I thought so there was syntax error "as I mentioned above I could not locate the function name in his code" and I tried to trim it to the function name I could see, but some how I did not get it to work... I am going to give it another shot with your suggested line of code...

                                Regards,

                                Jeff P.