December 6, 2013

Javascript: String has no method 'trim'

The trim method returns the string stripped of whitespace from both ends.
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:

No comments:

Post a Comment