Reference from steven levithan blog on Faster javascript trim
Trim the string in javascript for leading and ending spaces with the following funciton
<script type='text/javascript'>
function validate()
{
var str= document.getElementById('str').value;
if(str== '' || trim(str) == '')
{
alert('Invalid string');
return false;
}
else
{
return true;
}
}
function trim (str) {
str = str.replace(/^\s+/, '');
for (var i = str.length - 1; i >= 0; i--) {
if (/\S/.test(str.charAt(i))) {
str = str.substring(0, i + 1);
break;
}
}
return str;
}
</script>