How do I Capitalize First Letter of Each Word of a Field?

rhett1
Tera Expert

Is there a way that I can have an end-user enter a sentence into a normal string field and that each first letter of each word is made to be uppercase?

For example:

Risk no governance impacts the business.

TO:

Risk No Governance Impacts the Business.

1 ACCEPTED SOLUTION

WORKING Enhanced version that ignores capitalizing articles of the title:



function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '') {
          return;
    }


    var myText = newValue;
    var myTextInTitleCase = toTitleCase(myText);
    g_form.setValue('risk_name',myTextInTitleCase);


    function toTitleCase(e){
          var t=/^(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|vs?\.?|via)$/i;return e.replace(/([^\W_]+[^\s-]*) */g,
                function(e,n,r,i){
                      return r>0&&r+n.length!==i.length&&n.search(t)>-1&&i.charAt(r-2)!==":"&&i.charAt(r-1).search(/[^\s-]/)<0?e.toLowerCase():n.substr(1).search(/[A-Z]|\../)>-1?e:e.charAt(0).toUpperCase()+e.substr(1)
          }
    )};
}


View solution in original post

10 REPLIES 10

danielbilling
Kilo Guru

Hi Rhett,



you need a write a script the identify each word and then call a function that set first position toUpperCase then the rest of the string set by toLowerCase



//Daniel


poyntzj
Kilo Sage

I was going to say - however you like, but...


you could put it into a Script Include and call it


I have seen one here at the current client and that is in a Client Script.



Client Scipt can mean more loading on the client, more processing, could be slow ?


Script Include is passing data back and forth and if that is a large chunk is that quicker than doing it client side ?


Julian,



I appreciate what you have provided so far.   However, through much trial and error, I am still stumped on how to get this working.   If I were to go the path of a script include and client callable script, what would you recommend that look like?


If you are trying to call it from a client script, I would just include that function in the client script.



var myText = "text I want in title case";


var myTextInTitleCase = toTitleCase(myText);   // Result: "Text I Want In Title Case"



function toTitleCase(str) {


  return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});


}


Robert Beeman wrote:



If you are trying to call it from a client script, I would just include that function in the client script.



  1. var myText = "text I want in title case";  
  2. var myTextInTitleCase = toTitleCase(myText);   // Result: "Text I Want In Title Case"  
  3.  
  4. function toTitleCase(str) {  
  5.   return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});  
  6. }  


Using both your input Robert and Julian's here is what I came up with that ultimately worked as a good client script:



function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '') {
          return;
    }


    var myText = newValue;
    var myTextInTitleCase = toTitleCase(myText);
    g_form.setValue('risk_name',myTextInTitleCase);


    function toTitleCase(str) {
          return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
    }
}