7 Replies Latest reply on Aug 23, 2008 12:40 PM by (andyval)

    How to title case?

      I can't seem to get this to work: this was taken from pdfplanet (just trying to give credit to the guy who wrote it because I cant make heads or tails of it)

      I tried adding this javascripts to document javascripts:

      function toTitleCase(s) {

      // Regular expression for a letter, and followed by none or more letters
      var re1 = /(\w)(\w*)/;

      // Make an array of words that are in the input string
      var a1 = s.split(/\s+/g);

      // Loop through each word
      for (i = 0; i < a1.length; i ++ ) {

      // Split word into first letter
      // followed by the rest of word
      var a2 = a1[i].match(re1);

      // Capitalize first character of word
      a1[i] = a2[1].toUpperCase() + a2[2];

      // Alternatively, also convert rest of word to lower case
      //a1[i] = a2[1].toUpperCase() + a2[2].toLowerCase();

      }

      // Return new string built from words in array, separated by spaces
      return a1.join(" ");
      }

      the error i get in the javascript console is:

      7:Document-Level:toTitleCase
      s has no properties

      can someone help me find the error?
        • 1. Re: How to title case?
          Bernd Alheit Adobe Community Professional & MVP
          > I tried adding this javascripts to document javascripts:

          How did you do it?
          • 2. Re: How to title case?
            Patrick Leckey Level 3
            You need to pass a string as the argument.
            • 3. Re: How to title case?
              gkaiseril MVP & Adobe Community Professional
              As a matter of form, using a single character for a variable or parameter can sometimes cause problems with global variables used in the supplied JS library with Acrobat/Reader. So one should try to avoid using single letter variables or parameters whenever possible.

              The standard for array indexing is to start at 0 and not 1.
              • 4. Re: How to title case?
                gkaiseril MVP & Adobe Community Professional
                After running the code from George at PlanetPDF, I see there are a number of errors not caught like the missing parameter, a null parameter, numbers, and just a single space.

                A revised script could be:

                function toTitleCase(cString) {
                // add some error trapping code
                // assume missing parameter is a space
                if(cString == null) cString = ' ';
                // assume sn empty string is a space;
                if(cString == '') cString = ' ';
                // force passed parameter to a string to fix numbers passed as a parameter
                // and force to lower case
                s = cString.toString().toLowerCase();
                // do not process just a space
                if(cString == ' ') return '';
                else {
                // Regular expression for a letter, and followed by none or more letters
                var re1 = /(\w)(\w*)/;

                // Make an array of words that are in the input string
                var aString1 = cString.toString().split(/\s+/g);

                // Loop through each word
                for (i = 0; i < aString1.length; i ++ ) {

                // Split word into first letter
                // followed by the rest of word
                var aString2 = aString1[i].toString().match(re1);

                // Capitalize first character of word
                aString1[i] = aString2[1].toUpperCase() + aString2[2];

                }

                // Return new string built from words in array, separated by spaces
                return aString1.join(" ");
                }
                }

                // test the function for various types of parameters
                console.println('null as parameter: ' + toTitleCase() );
                console.println('empty string as parameter: ' + toTitleCase('') );
                console.println('space as parameter: ' + toTitleCase(' ') );
                console.println('single character as parameter: ' + toTitleCase("a") );
                console.println('number as parameter: ' + toTitleCase(4) );
                console.println('"test WORDS t12t3T456" as parameter: ' + toTitleCase('test WORDS t12t3T456') );
                • 5. Re: How to title case?
                  Level 1
                  i see that it works in the javascript console, but I can't seem to get it to work in the custom keystoke/format script area. is it suppose to be somewhere else?

                  edit:

                  thanks geo you are the man!

                  okay so i added the script geo kaiser revised to the document javascripts under advanced> document processing> document javascripts

                  then for the fields go to format>custom> custom format script>
                  add this to the javascript:

                  event.value = toTitleCase(event.value);

                  note: it doesn't change the format until the next field is selected or tabbed to. maybe someone can come up with a custom keystroke script but for now we have a working title / proper case script

                  Andy
                  • 6. Re: How to title case?
                    gkaiseril MVP & Adobe Community Professional
                    The format script is not applied until one leaves field. If you want to format as data is entered you will need to use the custom keystroke script and call this function to process each updated value.
                    • 7. Re: How to title case?
                      Level 1
                      how would i go about doing that