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

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