Wednesday, 30 January 2013

Convert String to Title case in Javascript

Hi,
Sometimes we need to convert user Name to proper casing, for that we can use below method.

i.e. JoHn METHEW will be converted to John Methew

/**
 *  Provie toProperCase method to string Class
 */
String.prototype.toProperCase = function() {
 return this.replace(/\w\S*/g, function(txt) {
  return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
 });
};

Usage:

"JoHn METHEW".toProperCase()

No comments:

Post a Comment