Wednesday, February 4, 2009

JavaScript: TRIM Function

Following function is very helpful to trim the leading as well as trailing spaces in a string. To make use of this function, first add the following code snip(declaration) in your JS library or in head section of HTML.

String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};



Then you may call this function like:

Example No:1

alert("Test Case 1:" + " StrinValue ".trim()+"sabah" )

Example No:2

< name="textfieldid" type="text" onchange="this.value=this.value.trim()">

No comments: