登录、注册获取焦点、失去焦点验证

<script>

		$('#phone').focus(function() {
			$('#msg').html("请输入合法的手机号");
			$('#msg').show();
		})
		$('#phone').blur(function() {
			var phone = $('#phone').val();
			var phonePattern = /^1(3|4|5|7|8)\d{9}$/;
			if(!phone || !(phonePattern.test(phone))) {
				$('#msg').html("您输入的手机号码有误");
				$('#msg').show();
			} else {
				$('#msg').hide();
			}

		})
		$('#password').focus(function() {
			$('#msg').html("输入6-20个字母、数字、下划线");
			$('#msg').show();
		})
		$('#password').blur(function() {
			var password = $('#password').val();
			var pPattern = /^(\w){6,20}$/;
			if(!password || !(pPattern.test(password))) {
				$('#msg').html("您输入的密码有误");
				$('#msg').show();
			} else {
				$('#msg').hide();
			}

		})

		$('#loginHandle').click(function() {
			var phone = $('#phone').val();
			var password = $('#password').val();
			var phonePattern = /^1(3|4|5|7|8)\d{9}$/;
			var pPattern = /^(\w){6,20}$/;
			//校验手机号的合法性
			if(phone == null || phone == '') {
				$('#msg').html("手机号不能为空");
				$('#msg').show();
				$("#phone").focus();
				return;
			};
			if(!(phonePattern.test(phone))) {
				$('#msg').html("您填写的手机号码有误,请重新填写");
				$('#msg').show();
				return;
			}
			/*矫正密码合法性*/
			if(password == null || password == '') {
				$('#msg').html("密码不能为空");
				$('#msg').show();
				return;
			}
			if(!(pPattern.test(password))) {
				$('#msg').html("只能输入6-20个字母、数字、下划线  ");
				$('#msg').show();
				return;
			}
			/*登录请求*/
			$.ajax({
				type: "post",
				url: "http://47.111.188.27:8080/gsgkzyl/user/login",
				xhrFields: {
					withCredentials: true
				},
				crossDomain: true,
				//contentType: "application/x-www-form-urlencoded; charset=UTF-8",
				data: {
					mobile: phone,
					password: password,
				},
				success: function(log) {
					console.log(log)
					if(log.status == 0) {
						SetCookie('userId',log.redisUserInfo.userId);
						layer.alert('登录成功', function() {
							window.location.href = "dynamic/news.html"
						})

					} else {
						$('#msg').show();
						$('#msg').html(log.msg);

					}
				}
			});
		})
	</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值