About This Code
Brief Description:
The JavaScript function trimBlanks
Contributor:
Andrew Jones
Last Modified:
17 Jun 2002
OpenNTF Disclaimer
All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.
Code / Description
The trimBlanks function is a utility routine that simply takes the field value and trims any extra space values. The JavaScript function follows:
function trimBlanks ( theString, repChar ) {
//this function replaces the spaces in a string with the provided
character
trimString="";
for ( i = 0; i < theString.length; i++) {
theChar=theString.substring ( i, i+1);
if ( theChar == " ") {
trimString += repChar
} else {
trimString+=theString.substring ( i, i+1);
}
}
return (trimString);
}
Usage / Example
Comments
Posted by Starrow Pan on 04/09/2007 05:56:00 AMmore efficient using RegExp
imitate the VBScript functions:
String.prototype.trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.ltrim = function()
{
return this.replace(/(^\s*)/g, "");
}
String.prototype.rtrim = function()
{
return this.replace(/(\s*$)/g, "");
}