function spc(str) { var l=str.length;//check length of string for(var i=0;i<l;i++) { if(str[0]==' ' || str[str.length-1]==' ')//check first & last character of password { document.getElementById("spa_error").innerHTML='Space key cannot be used for the first or last character of your password'; }else { document.getElementById("spa_error").innerHTML=''; } } }