- 爱易网页
 
                        - 
                            JavaSript
 
                        - 小弟我的form验证不好用了,验证提示异常还跳转 
 
                         
                    
                    
                    日期:2014-05-16  浏览次数:20545 次 
                    
                        
                         我的form验证不好用了,验证提示错误还跳转
<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>