This is the source for tree Javascript function to strip the spaces of a string. The first is a definition of the seperate function ltrim, rtrim and trim. These trim the spaces to the left of the string, the right of the string and both sides respectively.

function trim(s)
{
 return rtrim(ltrim(s));
}

function ltrim(s)
{
 var l=0;
 while(l < s.length && s[l] == ' ')
 { l++; }
 return s.substring(l, s.length);
}

function rtrim(s)
{
 var r=s.length -1;
 while(r > 0 && s[r] == ' ')
 { r-=1; }
 return s.substring(0, r+1);
}
If you are only going to use trim, you might want to use this, probably faster, defenition:

function trim(s)
{
 var l=0; var r=s.length -1;
 while(l < s.length && s[l] == ' ')
 { l++; }
 while(r > l && s[r] == ' ')
 { r-=1; }
 return s.substring(l, r+1);
}
Using r-=1; instead of r--; is done to prevent the XHTML validator from commenting on it. However you can freely interchange the two without the functionallity changing.

To also trim the newlines, tab characters etc. change the s[r] == ' ' into something like (s[r] == ' ' || s[r] == " " || s[r] == " ").

These functions are distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Feel free to copy, modify and distribute the above function defenitions.