用正则实现表单验证

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>用正则实现表单验证</title>
	<style>
		input{
			margin-top: 10px;
		}
		span{
			font-size: 14px;
			margin-left: 8px;
			color: #A2A2A2;
		}
		.agree{
			width: 150px;
			height: 40px;
			background: #FF6600;
			color: #fff;
			text-align: center;
			line-height: 40px;
			margin: 16px 0px 0px 70px;
		}
	</style>
</head>
<body>
	<span>&nbsp;&nbsp;&nbsp;会员名:</span><input type="text" / id="vip" "sub()" "add()" class="vip"><span id="an"></span><br>
	<span>登录密码:</span><input type="password" id="passone" "bdd()" "nub()"><span id="bn"></span><br>
	<span>确认密码:</span><input type="password" id="passtwo" "cdd()" "hub()"><span id="cn"></span><br>
	<div class="agree">同意协议并注册</div>
</body>
<script>
	function add(){
		an.innerHTML = "请输入正确的会员名格式";
	}
	function sub(){
		if (vip.value=="") {
			an.innerHTML="不能为空";
		}else{
			var reg = /^[a-zA-Z_\u4e00-\u9fa5]{6,12}$/;
			if (reg.test(vip.value)==false) {
				an.innerHTML="6-12个字符,推荐使用中文会员名";
			}else{
				an.innerHTML="格式正确";
			}
		}
	}
	function bdd(){
		bn.innerHTML = "请输入正确的密码格式";
	}
	function nub(){
		if (passone.value=="") {
			bn.innerHTML="不能为空";
		}else{
			var reg = /^(\w){6,20}$/;
			if (reg.test(passone.value)==false) {
				bn.innerHTML="格式不正确,请重新输入";
			}else{
				bn.innerHTML="格式正确";
			}
		}
	}


	function cdd(){
		cn.innerHTML = "请再次确认密码";
	}
	function hub(){
		if (passtwo.value=="") {
			cn.innerHTML="不能为空";
		}else{
			if (passone.value==passtwo.value) {
				cn.innerHTML="确认密码正确";
			}else{
				cn.innerHTML="两次输入的密码不一致,请重新输入";
			}
		}
	}
</script>
</html>

![表单全没填写实现效果](https://img-blog.csdnimg.cn/20190415192713884.png)
![表单填写正确效果](https://img-blog.csdnimg.cn/20190415192759145.png)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值