表单校验的另一种方法

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		span{color: red;font-size: 13px;display: none;}
	</style>
	<script type="text/javascript">
		
	</script>
</head>
<body>
	<!-- <div id="box"></div> -->
	<label for="">用户名:</label><input type="text" id="txt-name"><span id="tip-name">*用户名为8-12位数字字母</span><br>
	<label for="">密码:</label><input type="passworld" id="txt-pwd"><span id="tip-pwd">*密码为6位数字</span><br>
	<button>登录</button>
	<script type="text/javascript">
		var txtName=document.getElementById('txt-name');
		var txtPwd=document.getElementById('txt-pwd');
		var tipName=document.getElementById('tip-name');
		var tipPwd=document.getElementById('tip-pwd');

		//添加事件
		txtName.onblur=function () {
			var sName=txtName.value;
			var p=/^[a-zA-Z]\w{7,11}$/;
			if(!p.test(sName)){
				tipName.style.display='inline-block';
			}
		};

		txtPwd.onblur=function () {
			var sPwd=txtPwd.value;
			var p=/^\d{6}$/;
			if(!p.test(sPwd)){
				tipPwd.style.display='inline-block';
			}
		};
	</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_44606660/article/details/87779653