Wednesday, 30 January 2013

Convert String to Title case in Javascript

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


"JoHn METHEW".toProperCase()

No comments:

Post a Comment