注册页面----使用正则表达式判断输入是否合格

案例要求:
1、当光标离开input框的时候需要验证input框的内容是否合法
2、用户名:支持中文、字母、数字、““_”的组合,4-20个字符;用户名不能是数字
3、用户名长度只能在4-20个字符之间
4、密码:建议密码做—个强中弱的效果
5、二次密码:与—次密码必须—致
6、手机号码:需要验证手机号码的正确性(不能乱输入)
在这里插入图片描述
在这里插入图片描述
代码实现

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			*{
				margin: 0;
				padding: 0;
			}
			.container{
				width: 450px;
				height: 650px;
				/* background-color: #3E8E41; */
				position: absolute;
				top: 0;
				left: 0;
				right: 0;
				bottom: 0;
				margin: auto;
			}
			.box1{
				width: 90%;
				height: 50px;
				/* background-color: #000000; */
				
				position: absolute;
				top: 16px;
				left: 20px;
				margin: auto;
			}
			.box1>.username{
				width: 25%;
				height: inherit;
				/* background-color: #008000; */
				border: 2px solid #e5e5e5;
				position: absolute;
				top: 0;
				left: 0;
				margin: auto;
			}
			.box1>.username>p{
				width: 78px;
				height: inherit;
				/* font-size: large; */
				position: absolute;
				top: 12px;
				left: 18px;
				margin: auto;
			}
			.box1>input{
				width: 75%;
				height: inherit;
				position: absolute;
				border: 2px solid #e5e5e5;
				top: 0;
				right: 0;
				
			}
			.zs{
				width: 100%;
				height: 40px;
				position: absolute;
				top: 60px;
				left: 12px;
			}
			/* 密码 */
			.box2{
				width: 90%;
				height: 50px;
				/* background-color: #000000; */
				
				position: absolute;
				top: 116px;
				left: 20px;
				margin: auto;
			}
			.password{
				width: 25%;
				height: inherit;
				/* background-color: #008000; */
				border: 2px solid #e5e5e5;
				position: absolute;
				top: 0;
				left: 0;
				margin: auto;
			}
			.box2>.password>p{
				width: 78px;
				height: inherit;
				/* font-size: large; */
				position: absolute;
				top: 12px;
				left: 11px;
				margin: auto;
			}
			.box2>input{
				width: 75%;
				height: inherit;
				position: absolute;
				border: 2px solid #e5e5e5;
				top: 0;
				right: 0;
				
			}
			/* 确认密码 */
			.box3{
				width: 90%;
				height: 50px;
				/* background-color: #000000; */
				
				position: absolute;
				top: 190px;
				left: 20px;
				margin: auto;
			}
			.box3>.password>p{
				width: 78px;
				height: inherit;
				/* font-size: large; */
				position: absolute;
				top: 12px;
				left: 11px;
				margin: auto;
			}
			.box3>input{
				width: 75%;
				height: inherit;
				position: absolute;
				border: 2px solid #e5e5e5;
				top: 0;
				right: 0;
				
			}
			/* 手机 */
			.box4{
				width: 90%;
				height: 50px;
				/* background-color: #000000; */
				
				position: absolute;
				top: 270px;
				left: 20px;
				margin: auto;
			}
			.phone{
				width: 25%;
				height: inherit;
				/* background-color: #008000; */
				border: 2px solid #e5e5e5;
				position: absolute;
				top: 0;
				left: 0;
				margin: auto;
			}
			.box4>.phone>p{
				width: 85px;
				height: inherit;
				
				position: absolute;
				top: 14px;
				left: 12px;
				margin: auto;
			}
			.box4>input{
				width: 75%;
				height: inherit;
				position: absolute;
				border: 2px solid #e5e5e5;
				top: 0;
				right: 0;
				
			}
			/* 验证码 */
			.box5{
				width: 90%;
				height: 50px;
				/* background-color: #000000; */
				
				position: absolute;
				top: 352px;
				left: 20px;
				margin: auto;
			}
			.yzm{
				width: 25%;
				height: inherit;
				/* background-color: #008000; */
				border: 2px solid #e5e5e5;
				position: absolute;
				top: 0;
				left: 0;
				margin: auto;
			}
			.box5>.yzm>p{
				width: 85px;
				height: inherit;
				
				position: absolute;
				top: 14px;
				left: 18px;
				margin: auto;
			}
			.box5>input{
				width: 75%;
				height: inherit;
				position: absolute;
				border: 2px solid #e5e5e5;
				top: 0;
				right: 0;
				
			}
			/* 手机验证码 */
			.box6{
				width: 90%;
				height: 50px;
				/* background-color: #000000; */
				
				position: absolute;
				top: 432px;
				left: 20px;
				margin: auto;
			}
			.phoneyzm{
				width: 25%;
				height: inherit;
				/* background-color: #008000; */
				border: 2px solid #e5e5e5;
				position: absolute;
				top: 0;
				left: 0;
				margin: auto;
			}
			.box6>.phoneyzm>p{
				width: 85px;
				height: inherit;
				
				position: absolute;
				top: 14px;
				left: 7px;
				margin: auto;
			}
			.box6>input{
				width: 55%;
				height: inherit;
				position: absolute;
				border: 2px solid #e5e5e5;
				top: 0;
				right: 80px;
				
			}
			#hq{
				width: 20%;
				height: inherit;
				position: absolute;
				/* border: 2px solid #d0f8fa; */
				background-color: #d0f8fa;
				top: 2px;
				right: 0;
			}
			/* 勾选 */
			.box7{
				width: 90%;
				height: 50px;
				/* background-color: #000000; */
				
				position: absolute;
				top: 522px;
				left: 20px;
				margin: auto;
			}
			/* 注册 */
			.box8{
				width: 90%;
				height: 50px;
				/* background-color: #000000; */
				
				position: absolute;
				top: 566px;
				left: 20px;
				margin: auto;
			}
			
			.t{
				color: #FF0000;
				font-size:16px;
				position: absolute;
				top: 16px;
				right: 22px;
			}
		</style>
	</head>
	<body>
		<form action="../navigation/test.html" method="get">
			<div class="container">
				<div class="box1">
					<div class="username">
						<p><span>用 户 名</span></p>
					</div>
					<input type="text" name="username" id="username" placeholder="请输入您的用户名" value="" style="font-size: large;" onfocus="YHMonfocu()" onblur="YHMonblus()" />
					<div class="zs">
						<p><span style="color: #bababa;">支持中文、字母、数字、"_"、"-"的组合,4-20个字符</span></p>
					</div>
					<span id="YHMerror" class="t"></span>
				</div>
				
				<div class="box2">
					<div class="password">
						<p><span>设置密码</span></p>
					</div>
					<input type="text" name="password" id="password" value="" placeholder=" 建议至少使用两种字符" style="font-size: large;" onfocus="PWDonfocu()" onblur="PWDonblus()"/>
					<span id="PWDchoose" class="t"></span>
				</div>
				
				<div class="box3">
					<div class="password">
						<p><span>确认密码</span></p>
					</div>
					<input type="text" name="password" id="password1" value="" placeholder=" 确认密码"  style="font-size: large;" onfocus="PWD1onfocu()" onblur="PWD1onblus()"/>
					<span id="PWDchoose1" class="t"></span>
				</div>
				<div class="box4">
					<div class="phone">
						<p><span>中国 + 86</span></p>
					</div>
					<input type="text" name="phone" id="phone" value="" placeholder=" 建议常用手机" style="font-size: large;" onfocus="LXDHonfocu()" onblur="LXDHonblus()"/>
					<span id="LXDHerror" class="t"></span>
				</div>
				<div class="box5">
					<div class="yzm">
						<p><span>验 证 码</span></p>
					</div>
					<input type="text" name="yzm" id="yzm" value="" placeholder=" 请输入验证码" style="font-size: large;"/>
				</div>
				<div class="box6">
					<div class="phoneyzm">
						<p><span>手机验证码</span></p>
					</div>
					<input type="text" name="phoneyzm" id="phoneyzm" value="" placeholder=" 手机短信验证码" style="font-size: large;"/>
					<button type="button" id="hq">获取验证码</button>
				</div>
				<div class="box7">
					<input type="checkbox" name="read" id="read" value="" /> 我已阅读并同意<a href="">《京东用户注册协议》</a>
				</div>
				<div class="box8">
					<input type="submit" name="register" id="register" value="立即注册" style="width: 100%;height: 50px; background-color: #e5141f; color: white;
					border: white;"/>
				</div>
			</div>
		</form>
		
		<script type="text/javascript">
			function YHMonblus(){
				var username = document.getElementById("username");
				var re = /^[a-zA-Z0-9-_]{4,20}$/;
				var num = /^[0-9]*$/;
				if(username.value==""){
					document.getElementById('YHMerror').innerText="请输入用户名";
				}else if(username.value.length<4||username.value.length>20){
					console.log(username.value);
					document.getElementById('YHMerror').innerText="格式错误,长度应为6-18个字符";
				}else if(num.test(username.value)){
					console.log(num.test(username.value));
					document.getElementById('YHMerror').innerText="格式错误,不能为纯数字";
				}
			}
			function YHMonfocu(){
				document.getElementById('YHMerror').innerText = "";
			}
			
			function PWDonblus(){
				var password = document.getElementById("password");
				var re = /^(?=.*\d)(?=.*[a-zA-Z])[\da-zA-Z]{6,}$/;
				var num = /^[0-9]*$/;
				if(password.value==""){
					document.getElementById('PWDchoose').innerText="密码不能为空";
				}else if(password.value.length<6||password.value.length>20){
					document.getElementById('PWDchoose').innerText="格式错误,长度应为6-20个字符";
				}else if(password.value.length<=10&&num.test(password.value)){
					console.log(password.value);
					document.getElementById('PWDchoose').innerText="弱";
				}else if(password.value.length>10&&password.value.length<20&&num.test(password.value)){
					console.log(num.test(password.value));
					document.getElementById('PWDchoose').innerText="中";
				}else if(password.value.length>10&&re.test(password.value)){
					console.log(re.test(password.value));
					document.getElementById('PWDchoose').innerText="强";
				}
			}
			function PWDonfocu(){
				document.getElementById('PWDchoose').innerText = "";
			}
			
			
			function PWD1onblus(){
				var password = document.getElementById("password");
				var password1 = document.getElementById("password1");
				if(password1.value==""){
					document.getElementById('PWDchoose1').innerText="密码不能为空";
				}else if(password.value!=password1.value){
					document.getElementById('PWDchoose1').innerText="请输入相同密码";
				}
			}
			function PWD1onfocu(){
				document.getElementById('PWDchoose1').innerText = "";
			}
			
			function LXDHonblus(){
			        var phone=document.getElementById("phone");
			        var re = /^1\d{10}$/;
			        if(phone.value==""){
			            document.getElementById('LXDHerror').innerText="请输入联系电话";
			        }
			        else if(!re.test(phone.value)){
			            document.getElementById('LXDHerror').innerText="电话格式输入错误";
			        }
			        else {
			            document.getElementById('LXDHerror').innerText ="";
			        }
			    }
			    function LXDHonfocu(){
			        document.getElementById('LXDHerror').innerText ="";
			    }

		</script>
	</body>
</html>

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值