正则表达式第三章

//验证用户输入的是不是中文名字
//是中文名字,则为绿色,否则为红色
var userName = document.getElementById('userName');
userName.onblur = function(){
	var reg = /^[\u4e00-\u9fa5]{2,6}$/;//判断是否是中文名字
	if(reg.test(this.value)){
		this.style.backgroundColor = 'green';
	}else{
		this.style.backgroundColor = 'red';
	}
}

//大项目验证表单
//给我文本框,相应正则表达式,把结果进行显示
//通过正则表达式验证当前文本框是否匹配并显示结果
function checkInput(input,reg){
	//每个文本框注册失去焦点事件
	input.onblur = function(){
		if(reg.test(this.value)){
			this.nextElementSibling.innerText = '正确了';
			this.nextElementSibling.style.color = 'green';
		}else{
			this.nextElementSibling.innerText = '您输入的格式不正确';
			this.nextElementSibling.style.color = 'red';
		}
	}
}
//验证qq表达式
checkInput($('qq'),/^\d{5,11}$/);
//手机
checkInput($('qq'),/^[1][^24]\d{9}$/);
//邮箱
checkInput($('email'),/^[0-9a-zA-Z_.-]+[@][0-9a-zA-Z_.-]+([.][a-zA-Z]+){1,2}$/);
//座机号码
checkInput($('telphone'),/^\d{3,4}[-]\d{7,8}$/);
//中文名字
checkInput($('fullName'),/^[\u4e00-\u9fa5]$/);

猜你喜欢

转载自blog.csdn.net/weixin_42355871/article/details/83540996