I started JavaScript with the assumption that the string object already has a built-in function to trim preceding and proceeding spaces. Unlike C#/vb.net or T-SQL, the JavaScript string object doesn’t have this function. It’s just one of those subtle things that when religiously applied can save a few kilobytes down the wire especially in a high volume website with lots of input forms. Think of the server processing times that can be saved by performing this trimming in the browser rather than in the server-side.
I hope this turns out to be simple enough.
function TrimString(str) {
str = str.replace(/^[ ]+(.*)$/, '$1'); // Trims leading spaces
str = str.replace(/^(.*)[ ]+$/, '$1'); // Trims trailing spaces
return str;
}
God is in the details.
-Hillman Curtis