The below can be used to trim(remove) the empty spaces at either ends of a string. This can be used to validate a string or HTML form inputs for empty characters.
Just copy and paste the below code in the head section of the HTML page.
<script language="javascript">
function LTrim(str) {
for (var i=0; ((str.charAt(i)<=" ")&&(str.charAt(i)!="")); i++);
return str.substring(i,str.length);
}
function RTrim(str) {
for (var i=str.length-1; ((str.charAt(i)<=" ")&&(str.charAt(i)!="")); i--);
return str.substring(0,i+1);
}
function Trim(str) {
return LTrim(RTrim(str));
}</script>
Usage Example:<a href="javascript: alert(Trim(' JavaScript Trim '));">Click here</a>
Output:JavaScript Trim