Setting a Dynamics CRM field’s value to Title Case using JavaScript

In a thread on one of the community forms, Debra asked how to properly add some JavaScript she had found on another forum post to make the first letter in each word uppercase (while the remainder is lower-case). This is called title-case, by the way.

Taking the code that Debra mentioned, I modified it a bit, and ended up with this:

function upperCaseField(executionContext) {
    var field = executionContext.getEventSource()


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

It is configured on an individual field like this:


It is most imperative that you check the box for Pass execution context as first parameter.  If you do not, then the function will fail.

If you wish to add this function to multiple fields, you can do so using this technique, as mentioned in that thread by @aileengusni:

function form_OnLoad() {