3 Replies Latest reply on Apr 27, 2015 8:01 AM by gkaiseril

    Looking for a simple way to convert a string to title case

    BassPlayer19

      New to LiveCycle and Javascript.  Looking for a simple way to convert a string to title case, except acronyms.  Currently using the the following, it converts acronyms to lower case:

      var str  =  this.rawValue;

      var upCase = str.split(" ");

      for(i=0; i < upCase.length; i++) {

      upCase[i] = upCase[i].substr(0,1).toUpperCase() + upCase[i].substr(1).toLowerCase();

      }

      this.rawValue = upCase.join(' ');

        • 1. Re: Looking for a simple way to convert a string to title case
          gkaiseril MVP & Adobe Community Professional

          You have to make sure you are not adjusting acronyms, so you need to add code to exclude them from having their case changed. This means you need to add in an array of acronyms that you do not want changed and then search to see if you have any known acronyms and exclude them from the processing. This will only work for known acronyms and unknown or expected acronyms will be processed.

          • 2. Re: Looking for a simple way to convert a string to title case
            BassPlayer19 Level 1

            Thanks for the reply.

             

            Found the following script in a forum, which works fine as a "custom validation script" in the.pdf version of my form.  However, it will not work in LiveCycle?  The problem seems to be with

             

            "return str.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g"

             

            function toTitleCase(str) {
            var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;

                return str.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function(match, index, title){
            if (index > 0 && index + match.length !== title.length &&
              match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
            (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
            title.charAt(index - 1).search(/[^\s-]/) < 0) {
            return match.toLowerCase();
            }
            if (match.substr(1).search(/[A-Z]|\../) > -1) {
              return match;
               }
              return match.charAt(0).toUpperCase() + match.substr(1);
            });
            };

            event.value = toTitleCase(event.value);

            • 3. Re: Looking for a simple way to convert a string to title case
              gkaiseril MVP & Adobe Community Professional

              Since this is a formatting of the value, why not use the "Format" and a custom format?

               

              Are you running this script using the JavaScript linaguage?

               

              I would make the function a document level JavaScript in LIveCycle.

               

              I think the issue is the function being defined on the fly within a function.