我想做个简单的注册页面,所填写的信息需要验证,可是我的验证按钮和提交按钮怎么成了同一个按钮了,也就是说“检查用户名是否存在”按钮,我想做成<html:errors/>这种验证,“提交”按钮我想用页面上的JavaScript验证,可是现在点击“检查用户名是否存在”按钮也进行了JavaScript验证,并没有提交到我规定的action中,请高手帮我看看是怎么回事,多谢多谢!!
我的代码:
<%@page pageEncoding="GBK" contentType="text/html; charset=GBK" %>
<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>
<%@ taglib uri="/ljtag" prefix="mytag" %>
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
<title>用户注册</title>
</head>
<script language="JavaScript">
function check(){
document.forms[0].action="/checkusername.do";
}
function validate(){
var x = document.forms[0];
if(x.username.value==""){
alert("请输入用户名!");
return false;
}
else if(x.password.value==""){
alert("密码不能为空!");
return false;
}
else if(x.password.value != x.password2.value){
alert("密码不相符,请重新输入!");
return false;
}
else if(x.sex[0].checked=="" && x.sex[1].checked==""){
alert("请选择性别!");
return false;
}
else if(x.leaveword.value==""){
alert("请留言!");
return false;
}
else {
return true;
}
}

</script>
<body>
<h1 align="center"><font color="red">请填写注册信息</font></h1><hr>
<html:form action="/user_logon.do" method="post" onsubmit="return validate();">
<center>
    <table border="0">
     <tr>
      <td>用&nbsp;户&nbsp;名&nbsp;&nbsp;</td>
      <td><html:text property="username"></html:text></td>
      <td><input type="submit" name="check" value="查看用户名是否可用" onclick="check();"></td>
     </tr>
     <tr>
      <td></td>
      <td><html:errors/></td>
     </tr>
             <tr>
              <td>密&nbsp;&nbsp;&nbsp;码&nbsp;&nbsp;</td>
              <td><html:password property="password"></html:password></td>
             </tr>
             <tr>
              <td>确认密码&nbsp;&nbsp;</td>
              <td><html:password property="password2"></html:password></td>
             </tr>
             <tr>
              <td>性别</td>
              <td>
               <html:radio property="sex" value="male">男</html:radio>
               <html:radio property="sex" value="female">女</html:radio>
              </td>
             </tr>
             <tr>
             <td>请选择居住地</td>
              <td></td>
             </tr>
             <tr>
              <td colspan="2">欢迎注册,请留言!</td>
             </tr>
             <tr>
             <td colspan="2"><html:textarea property="leaveword"></html:textarea></td>
     </tr>
            </table><br><br>
            <html:submit property="submit" value="注册"></html:submit>
            <html:reset property="reset" value="重置"></html:reset>
            </center>
    </html:form>
</body>
</html:html>

解决方案 »

  1.   

    <html:form   action="/user_logon.do"   method="post"   onsubmit="return   validate();"> 
    把这句里的onsubmit事件去掉,放入<html:submit   property="submit"   value="注册"> 的onclick事件中
      

  2.   

    表单验证这个不要用submit按钮撒,,,用普通按钮,加一个onclick事件,,如: onclick="return 方法;"
    在表单验证的方法里面,,如果验证通过的话..return true;反之return false 就行了
      

  3.   

    验证用户名没必要做成表单提交那么复杂啊,弹出框或者AJAX更显得简单清晰!
      

  4.   

    button的类型错了啊。都说了是普通按钮了,你还type = "submit",应该是type = "button"