Expand my Community achievements bar.

Applications for the Community Advisor Program Class of 2025 are NOW OPEN – Apply Today!
SOLVED

How can you use the 'toProperCase' function on a variable

Avatar

Level 1

Hi,

 

We would like to use the 'toProperCase' function on the <%= recipient.preferredName %> 

 

But unsure of how to format it? Please could someone help!

 

Thanks,

Tutu

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@Tutsaa 

First define the function like this

<%

String.prototype.toProperCase = function() {
var words = this.split(' ');
var results = [];
for (var i = 0; i < words.length; i++) {
var letter = words[i].charAt(0).toUpperCase();
results.push(letter + words[i].slice(1));
}
return results.join(' ');
};

%>

Then you can use it like this 

<%= recipient.preferredName.toProperCase() %>

 


     Manoj
     Find me on LinkedIn

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

@Tutsaa 

First define the function like this

<%

String.prototype.toProperCase = function() {
var words = this.split(' ');
var results = [];
for (var i = 0; i < words.length; i++) {
var letter = words[i].charAt(0).toUpperCase();
results.push(letter + words[i].slice(1));
}
return results.join(' ');
};

%>

Then you can use it like this 

<%= recipient.preferredName.toProperCase() %>

 


     Manoj
     Find me on LinkedIn

Avatar

Employee Advisor

Could also do something like this... (The joy of JavaScript, 1000 ways to do the same thing!)

 

<%​
function nameProper() {​
  var name = recipient.firstName;​
  var propName = name.chartAt(0).toUpperCase() + name.slice(1);​
}%>​

Dear <%= nameProper %>,

 

Dear <%= nameProper %>,

Avatar

Level 9

Hi @Tutsaa ,

 

You can use a simple version also:

<%= recipient.preferredName.substring(0,1).toUpperCase()+ recipient.preferredName.substring(1,100).toLowerCase() %>

 

Thanks,

Jyoti Yadav