<script type="text/javascript">
var code;
function createCode() {
code = "";
var codeLength = 4;// 验证码的长度
var checkCode = document.getElementById("checkCode");
checkCode.value = "";
var selectChar = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 'a', 'b', 'c',
'd', 'e', 'f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
for ( var i = 0; i < codeLength; i++) {
var charIndex = Math.floor(Math.random() * 36);
code += selectChar[charIndex];
}
if (code.length != codeLength) {
createCode();
}
checkCode.value = code;
}
function registration(formObject) {
        var codeToUp = code.toUpperCase();
var inputCode = document.getElementById("code1").value.toUpperCase();

if (document.forms[0].username.value=="") {
alert("友情提示:用户名不能为空");
return false;
} if (document.forms[0].password.value.length < 3) {
alert("友情提示:密码不能小于3");
return false;
}
if (formObject.username.value == "") {
alert("友情提示:用户名不能为空");
return false;
}
if (formObject.password.value == "") {
alert("友情提示:密码不能为空");
return false;
}


if (inputCode.length <= 0) { alert("请输入验证码");
return false;
}if (inputCode != codeToUp) { alert("验证码输入错误!");
createCode();
return false;
}else if (inputCode != codeToUp) {
return true;
}  formObject.submit(); }
</script>
<body onload="createCode();">
<a href="/DotaTest/selectLang.do?lang=zh_CN">CH</a>
<a href="/DotaTest/selectLang.do?lang=en_US">EN</a>
<html:form action="/userLogin"> 


<bean:message key="username"/><html:text property="username"/>
<br>
<bean:message key="password"/><html:password property="password"/><br>
<br><bean:message key="code"/><input type="text" id="code1"/>
<input type="text" id="checkCode" class="code" style="width: 55px" />
<a href="#" onclick="createCode()"><bean:message key="code1"/></a>
<br>
<input type="submit" value="<bean:message key="submit"/>" id="button1"
onclick="registration(this.form)" />

        <br>
<input type="button" value="<bean:message key="reset"/>" name="submit1"
onclick="reset(this.form)" /> </html:form></td>
  </tr>
</table></body>
</html>十分感谢!!

解决方案 »

  1.   

    onclick="registration(this.form)"改成:
    onclick="return registration(this.form)"
      

  2.   

    三种解决方式:
    1把你验证方法在form的onsubmit()里调用
    2.如1楼所说submit的按钮的onclick事件要return 一个你验证脚步的结果,你的写法只是用来运行一次方法而没有起到验证的作用
    3.把提交按钮改为type为button的普通按钮然后在验证脚本了在通过验证的情况下手动提交表单