javascript注册页面焦点

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script language="javascript" src="./shijian.js"></script>
	</head>
	<body>
		<div id="reg">
			<label>用户名:<input id="user" name="user" type="text" /><span></span></label><br/>
			<label>密码:<input id="pwd" name="pwd" type="password" /><span></span></label><br/>
			<label>确认密码:<input id="pwd2" name="pwd2" type="password" /><span></span></label><br/>
			<label>手机号码:<input id="tel" name="tel" type="text" maxlength='11'/><span></span></label><br/>
			<label>电子邮件:<input id="email" name="email" type="text" /><span></span></label><br/>
			<label>QQ号码:<input id="qq" name="qq" type="text" /><span></span></label><br/>
			<label><button id="sub" type="submit">提交</button>&nbsp;&nbsp;<button id="res" type="reset">重置</button></label>	
		</div>
		<script language="javascript">
			EventUtil.addHandler(window,'load',addblur($('user')));
			EventUtil.addHandler(window,'load',addblur($('pwd')));
			EventUtil.addHandler(window,'load',addblur($('pwd2')));
			EventUtil.addHandler(window,'load',addblur($('tel')));
			EventUtil.addHandler(window,'load',addblur($('email')));
			EventUtil.addHandler(window,'load',addblur($('qq')));
			function $(obj){
				return document.getElementById(obj);
			}
			function addblur(obj){
				EventUtil.addHandler(obj,'blur',isEmpty);
			}
			function isEmpty(){
				if(this.value==''){
					this.nextElementSibling.innerHTML='不能为空';
					this.style.backgroundColor="lightblue";
				}else{
					e=EventUtil.getEvent(event);
					target=EventUtil.getTarget(e);
					if(target===user){
						if(!/^\w*$/.test(target.value.trim())){
							target.focus();
							target.style.backgroundColor='red';
							target.nextElementSibling.innerHTML='只能是数字或字母';
						}else if(target.value.trim().length<4||target.value.trim.lenght>13){
							target.focus();
							target.style.backgroundColor='red';
							target.nextElementSibling.innerHTML='请填写4-13个字符';
						}else{
							target.style.backgroundColor='white';
							target.nextElementSibling.innerHTML='正确';
						}
					}
					if(target===pwd){
						if(/^(\d*|[a-z]*)$/.test(target.value.trim())){
							target.focus();
							target.style.backgroundColor='red';
							target.nextElementSibling.innerHTML='不能只是纯数字或字母';
						}else if(target.value.trim().length<7||target.value.trim().length>20){
							target.focus();
							target.style.backgroundColor='red';
							target.nextElementSibling.innerHTML='介于6-20个字符';
						}else{
							target.style.backgroundColor='white';
							target.nextElementSibling.innerHTML='正确';
						}
					}
					if(target===pwd2){
						if(target.value.trim()!=document.getElementById('pwd').value){
							target.focus();
							target.style.backgroundColor='red';
							target.nextElementSibling.innerHTML='两次密码不一致';
						}else{
							target.style.backgroundColor='white';
							target.nextElementSibling.innerHTML='正确';
						}
					}
					if(target==tel){
						if(!/^(13|14|15|16|17|18|19)\d{9}$/.test(target.value.trim())){
							target.focus();
							target.style.backgroundColor='red';
							target.nextElementSibling.innerHTML='电话填写不正确';
						}else{
							target.style.backgroundColor='white';
							target.nextElementSibling.innerHTML='正确';
						}
					}
				}
			}
		</script>
	</body>
</html>

利用事件和事件目标元素显示不同的互动,用netxtElementSibling来获取span然后增加内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值