Pages

December 6, 2013

Javascript: String has no method 'trim'



The trim method returns the string stripped of whitespace from both ends.
string.trim()
If the trim function it's not natively available, the following code will create it:
  
if (!String.prototype.trim) {
 String.prototype.trim = function () {
     return this.replace(/^\s+|\s+$/gm, '');
 };
  }

With jquery it's very easy:
  $.trim()