JavaScript完成简单的表单校验案例

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册界面</title>
<script type="text/javascript">
	function checkForm(){
		//alert("aa");
		/* 检验用户名 */
		//1.获取用户输入的数据
		var uValue = document.getElementById("user").value;
		//alert(uvalue);
		if(uValue == ""){
			//2.给出错误提示信息
			alert("用户名不能为空");
			return  false;
		}
	    /* 检验密码 */
	    var pValue = document.getElementById("password").value;
	    if(pValue == ""){
	    	alert("密码不能为空");
	    	return false;
	    }
		
	    /* 检验确认密码 */
	    var rpValue = document.getElementById("repassword").value;
	    if(rpValue != pValue){
	    	alert("两次密码输入不一致!");
	    	return false;
	    }
	}
</script>
</head>
<body>
	<form action="#" method="get" name="regForm" onsubmit="return checkForm()">
		<table >
			<tr>
				<td>用户名</td>
				<td><input type="text" name="user" size="34px" 
				    id="user"></td>
			</tr>
			<tr>
				<td>密码</td>
				<td><input type="password" name="password" size="34px"
					id="password"></td>
			</tr>
			<tr>
				<td>确认密码</td>
				<td><input type="password" name="repassword" size="34px"
					id="repassword"></td>
			<tr>

				<td colspan="2"><input type="submit" value="注册"></td>
			</tr>
		</table>
	</form>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/fwyfwyfwy/article/details/83544354