JS+jquery正则简易校验

 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<body>
		<form action="购物车.html" id="regForm">
			<span>用户名</span>:<input type="text" name="username" reg="/^\w{5,10}$/" id="username" /> <span></span> <br />
			<span>密码</span>:<input type="password" name="pwd" reg="/^\w{5,10}$/" id="pwd" />
			<span></span> <br />
			<span>手机号</span>:<input type="text" name="phone"
				reg="/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/" id="phone" /> <span></span>
			<br />
			<span>身份证号</span>:<input type="text" name="idcard" reg="/^\d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$/"
				id="idcard" /> <span></span> <br />

			<span>邮箱</span>:<input type="text" name="email" id="email"
				reg="/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/" /> <span></span> <br />
			<input type="button" value="注册" id="regBtn" /><br />
		</form>
		<script src="./jquery-3.5.0.min.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
		var jilu=true;
			$("input[type='text'],input[type='password']").blur(function(){//给所有文本框绑定失去焦点事件
				var a = this.value;//获取录入的内容
				if (a != "" && a != null && a != undefined) {
					var b=$(this).attr("reg")//attr函数  getAttribute setAttribute
					b=eval(b)
					if(b.test(a)){
						$(this).next().html("")
					}else{
						$(this).next().html("格式错误")
						jilu=false
					}
				}else{
					$(this).next().html("不能为空")
					jilu=false
				}
			})
			$("#regBtn").click(function(){
				jilu=true
				var inpqq=$("input[type='text'],input[type='password']");
				inpqq.each(function(){
					$(this).trigger("blur")//触发对象失去焦点的事件
				})
				// for(var i=0;i<inp.length;i++){
				// 	$(inp[i]).trigger('blur')
				// }
				if(jilu){
					$("#regForm").submit()
				}
			})
	    </script>
	</body>
</html>

简易代码,仅作参考,请勿其他用途

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值