1 <script type="text/javascript"> 2 function checklogin1(){ 3 if(document.login1.username.value=="" || document.login1.username.value.replace(/s/g,'')==''){ 4 alert('请输入用户名!'); 5 document.login1.username.focus(); 6 return false; 7 }else if(document.login1.password.value==""){ 8 alert('请输入密码!'); 9 document.login1.password.focus(); 10 return false; 11 }else if(document.login1.answer.value==""){ 12 alert('请输入问题验证!'); 13 document.login1.answer.focus(); 14 return false; 15 }else if(document.login1.answer.value !="199"){ 16 alert('问题验证错误!'); 17 document.login1.answer.focus(); 18 return false; 19 } 20 21 } 22 </script>
1 <form name="login1" method="post" action=" " onsubmit="return checklogin1();"> 2 <div class="regc"> 3 <span class="left">用户名:</span><input class="inputstyle1" type="text" id="username" name="username" /> 4 <span class="left">密码:</span><input class="inputstyle1" type="password" id="password" name="password" /> 5 <span class="left">问题验证</span><input type="text" name="answer" id="answer" size="20" class="textInt inputstyle1"/> 6 <input type="submit" id="submit" name="submit" value="登录" class="newFormBtn_red2"/> 7 </div> 8 </form>
去除空格
1 document.login1.username.value.replace(/s/g,'')==''
PHP补充:
1 if(mysql_affected_rows()>0){ 2 echo "<script>alert('用户名已存在');window.history.go(-1);</script>"; 3 // header("refresh:0;url=./register.php"); 4 die; 5 }
window.history.go(-1);也可避免刷新后表单中数据丢失