用正则表达式来判断手机号、地址、身份证号、邮箱等格式是否正确

全部代码

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>

	<body>
		<span>QQ:</span><input type="text" id="qq" placeholder="输入QQ号" onblur="isQq()" /><span id="qqShow"></span><br />
		<span>手机号:</span><input type="text" id="tel" placeholder="输入手机号" onblur="isTel()" /><span id="telShow"></span><br />
		<span>邮箱:</span><input type="text" id="email" placeholder="输入邮箱" onblur="isEmail()" /><span id="emailShow"></span><br />
		<span>地址:</span><input type="text" id="address" placeholder="输入地址" onblur="isAddress()" /><span id="addressShow"></span><br />
		<span>邮编:</span><input type="text" id="postcode" placeholder="输入邮编" onblur="isPostcode()" /><span id="postcodeShow"></span><br />
		<span>银行卡号:</span><input type="text" id="bankCard" placeholder="输入银行卡号" onblur="isBankCard()" /><span id="bankCardShow"></span><br />
		<span>学号:</span><input type="text" id="studentID" placeholder="输入学号" onblur="isStudentID()" /><span id="studentIDShow"></span><br />
		<span>车牌号:</span><input type="text" id="carNumber" placeholder="输入车牌号" onblur="isCarNumber()" /><span id="carNumberShow"></span><br />
		<span>快递单号:</span><input type="text" id="courierNumber" placeholder="输入快递单号" onblur="isCourierNumber()" /><span id="courierNumberShow"></span><br />
		<span>验证码:</span><input type="text" id="captcha" placeholder="输入验证码" onblur="isCaptcha()" /><span id="captchaShow"></span><br />
		<span>ISBN号:</span><input type="text" id="isbn" placeholder="输入ISBN号" onblur="isIsbn()" /><span id="isbnShow"></span><br />
		<span>网址:</span><input type="text" id="website" placeholder="输入网址" onblur="isWebsite()" /><span id="websiteShow"></span><br />
		<span>IPV4地址:</span><input type="text" id="ipv4" placeholder="输入IPV4地址" onblur="isIpv4()" /><span id="ipv4Show"></span><br />
		<span>IPV6地址:</span><input type="text" id="ipv6" placeholder="输入IPV6地址" onblur="isIpv6()" /><span id="ipv6Show"></span><br />
		<span>出生年月:</span><input type="text" id="birthday" placeholder="输入出生年月" onblur="isBirthday()" /><span id="sbirthdayShow"></span><br />
		<span>姓名:</span><input type="text" id="name" placeholder="输入姓名" onblur="isName()" /><span id="nameShow"></span><br />

		<script type="text/javascript">
			//QQ号 
			function isQq() {
				var qq = document.getElementById("qq").value;
				var isQq = /^[1-9]{5,11}$/;
				if(isQq.test(qq)) {
					document.getElementById("qqShow").innerText = "验证通过";
					document.getElementById("qqShow").style.color = "green";
				} else {
					document.getElementById("qqShow").innerText = "输入格式错误!";
					document.getElementById("qqShow").style.color = "red";
				}
			}
			//手机号
			function isTel() {
				var tel = document.getElementById("tel").value;
				var isTel = /^[1][0-9]{10}$/;
				if(isTel.test(tel)) {
					document.getElementById("telShow").innerText = "验证通过";
					document.getElementById("telShow").style.color = "green";
				} else {
					document.getElementById("telShow").innerText = "输入格式错误!";
					document.getElementById("telShow").style.color = "red";
				}
			}
			//邮箱
			function isEmail() {
				var email = document.getElementById("email").value;
				var isEmail = /^[a-zA-Z0-9]{5,15}@[a-zA-Z0-9]{2,8}\.[a-zA-Z]{2,3}$/;
				if(isEmail.test(email)) {
					document.getElementById("emailShow").innerText = "验证通过";
					document.getElementById("emailShow").style.color = "green";
				} else {
					document.getElementById("emailShow").innerText = "输入格式错误!";
					document.getElementById("emailShow").style.color = "red";
				}
			}
			//地址
			function isAddress() {
				var address = document.getElementById("address").value;
				var isAddress = /[\u4e00-\u9fa5 0-9 -]+/;
				if(isAddress.test(address)) {
					document.getElementById("addressShow").innerText = "验证通过";
					document.getElementById("addressShow").style.color = "green";
				} else {
					document.getElementById("addressShow").innerText = "输入格式错误!";
					document.getElementById("addressShow").style.color = "red";
				}
			}
			//邮编
			function isPostcode() {
				var postcode = document.getElementById("postcode").value;
				var isPostcode = /^[1-9][0-9]{5}$/;
				if(isPostcode.test(postcode)) {
					document.getElementById("postcodeShow").innerText = "验证通过";
					document.getElementById("postcodeShow").style.color = "green";
				} else {
					document.getElementById("postcodeShow").innerText = "输入格式错误!";
					document.getElementById("postcodeShow").style.color = "red";
				}
			}
			//银行卡号
			function isBankCard() {
				var bankCard = document.getElementById("bankCard").value;
				var isBankCard = /^[6][0-9]{18}$/;
				if(isBankCard.test(bankCard)) {
					document.getElementById("bankCardShow").innerText = "验证通过";
					document.getElementById("bankCardShow").style.color = "green";
				} else {
					document.getElementById("bankCardShow").innerText = "输入格式错误!";
					document.getElementById("bankCardShow").style.color = "red";
				}
			}
			//学号
			function isStudentID() {
				var studentID = document.getElementById("studentID").value;
				var isStudentID = /^[0609]{4}((0[1-9])|(1[1-9])|(2[0]))[0-9]{4}$/;
				if(isStudentID.test(studentID)) {
					document.getElementById("studentIDShow").innerText = "验证通过";
					document.getElementById("studentIDShow").style.color = "green";
				} else {
					document.getElementById("studentIDShow").innerText = "输入格式错误!";
					document.getElementById("studentIDShow").style.color = "red";
				}
			}
			//车牌号
			function isCarNumber() {
				var carNumber = document.getElementById("carNumber").value;
				var isCarNumber = /^[\u4e00-\u9fa5][A-Z]\·[A-Z0-9]{5}$/;
				if(isCarNumber.test(carNumber)) {
					document.getElementById("carNumberShow").innerText = "验证通过";
					document.getElementById("carNumberShow").style.color = "green";
				} else {
					document.getElementById("carNumberShow").innerText = "输入格式错误!";
					document.getElementById("carNumberShow").style.color = "red";
				}
			}
			//快递单号
			function isCourierNumber() {
				var courierNumber = document.getElementById("courierNumber").value;
				var isCourierNumber = /^[JD]{2}[0-9]{13}$/;
				if(isCourierNumber.test(courierNumber)) {
					document.getElementById("courierNumberShow").innerText = "验证通过";
					document.getElementById("courierNumberShow").style.color = "green";
				} else {
					document.getElementById("courierNumberShow").innerText = "输入格式错误!";
					document.getElementById("courierNumberShow").style.color = "red";
				}
			}
			//验证码
			function isCaptcha() {
				var captcha = document.getElementById("captcha").value;
				var isCaptcha = /^(([a-zA-Z0-9]{4})|([a-zA-Z0-9]{6}))$/;
				if(isCaptcha.test(captcha)) {
					document.getElementById("captchaShow").innerText = "验证通过";
					document.getElementById("captchaShow").style.color = "green";
				} else {
					document.getElementById("captchaShow").innerText = "输入格式错误!";
					document.getElementById("captchaShow").style.color = "red";
				}
			}
			//ISBN号
			function isIsbn() {
				var isbn = document.getElementById("isbn").value;
				var isIsbn = /^((97[8])|(97[9]))-[0-9]-[0-9]{3}-[0-9]{5}-[0-9]$/; //978,979开头 后面横杠加数字978-x-xxx-xxxxx-x
				if(isIsbn.test(isbn)) {
					document.getElementById("isbnShow").innerText = "验证通过";
					document.getElementById("isbnShow").style.color = "green";
				} else {
					document.getElementById("isbnShow").innerText = "输入格式错误!";
					document.getElementById("isbnShow").style.color = "red";
				}
			}
			//网址
			function isWebsite() {
				var website = document.getElementById("website").value;
				var isWebsite = /^(http|https)(:\/\/)((www)\.)*[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)[/0-9a-zA-Z:\.?#=]*$/;
				if(isWebsite.test(website)) {
					document.getElementById("websiteShow").innerText = "验证通过";
					document.getElementById("websiteShow").style.color = "green";
				} else {
					document.getElementById("websiteShow").innerText = "输入格式错误!";
					document.getElementById("websiteShow").style.color = "red";
				}
			}
			//IPV4地址
			function isIpv4() {
				var ipv4 = document.getElementById("ipv4").value;
				var isIpv4 = /^((([0-9]{1,2})|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]{1,2})|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))$/;
				if(isIpv4.test(ipv4)) {
					document.getElementById("ipv4Show").innerText = "验证通过";
					document.getElementById("ipv4Show").style.color = "green";
				} else {
					document.getElementById("ipv4Show").innerText = "输入格式错误!";
					document.getElementById("ipv4Show").style.color = "red";
				}
			}
			//IPV6地址
			function isIpv6() {
				var ipv6 = document.getElementById("ipv6").value;
				var isIpv6 = /^(([a-fA-F0-9]{1,4}[:]){7}[a-fA-F0-9]{1,4})$/;
				if(isIpv6.test(ipv6)) {
					document.getElementById("ipv6Show").innerText = "验证通过";
					document.getElementById("ipv6Show").style.color = "green";
				} else {
					document.getElementById("ipv6Show").innerText = "输入格式错误!";
					document.getElementById("ipv6Show").style.color = "red";
				}
			}
			//出生年月日
			function isBirthday() {
				var birthday = document.getElementById("birthday").value;
				var isBirthday = /^[0-9]{4}年((0[1-9])|(1[0-2]))月((0[1-9])|(1[0-9])|(2[0-9])|(3[0-1]))日$/;
				if(isBirthday.test(birthday)) {
					document.getElementById("birthdayShow").innerText = "验证通过";
					document.getElementById("birthdayShow").style.color = "green";
				} else {
					document.getElementById("birthdayShow").innerText = "输入格式错误!";
					document.getElementById("birthdayShow").style.color = "red";
				}
			}
			//姓名
			function isName() {
				var name = document.getElementById("name").value;
				var isName = /^[\u4e00-\u9fa5]{2,}([·]*[\u4e00-\u9fa5])*$/;
				if(isName.test(name)) {
					document.getElementById("nameShow").innerText = "验证通过";
					document.getElementById("nameShow").style.color = "green";
				} else {
					document.getElementById("nameShow").innerText = "输入格式错误!";
					document.getElementById("nameShow").style.color = "red";
				}
			}
		</script>
	</body>
</html>

QQ号

//QQ号 
			function isQq() {
				var qq = document.getElementById("qq").value;
				var isQq = /^[1-9]{5,11}$/;
				if(isQq.test(qq)) {
					document.getElementById("qqShow").innerText = "验证通过";
					document.getElementById("qqShow").style.color = "green";
				} else {
					document.getElementById("qqShow").innerText = "输入格式错误!";
					document.getElementById("qqShow").style.color = "red";
				}
			}

手机号

function isTel() {
				var tel = document.getElementById("tel").value;
				var isTel = /^[1][0-9]{10}$/;
				if(isTel.test(tel)) {
					document.getElementById("telShow").innerText = "验证通过";
					document.getElementById("telShow").style.color = "green";
				} else {
					document.getElementById("telShow").innerText = "输入格式错误!";
					document.getElementById("telShow").style.color = "red";
				}
			}

邮箱

//邮箱
			function isEmail() {
				var email = document.getElementById("email").value;
				var isEmail = /^[a-zA-Z0-9]{5,15}@[a-zA-Z0-9]{2,8}.[a-zA-Z]{2,3}$/;
				if(isEmail.test(email)) {
					document.getElementById("emailShow").innerText = "验证通过";
					document.getElementById("emailShow").style.color = "green";
				} else {
					document.getElementById("emailShow").innerText = "输入格式错误!";
					document.getElementById("emailShow").style.color = "red";
				}
			}

地址

//地址
			function isAddress() {
				var address = document.getElementById("address").value;
				var isAddress = /[\u4e00-\u9fa5 0-9 -]+/;
				if(isAddress.test(address)) {
					document.getElementById("addressShow").innerText = "验证通过";
					document.getElementById("addressShow").style.color = "green";
				} else {
					document.getElementById("addressShow").innerText = "输入格式错误!";
					document.getElementById("addressShow").style.color = "red";
				}
			}

邮编

//邮编
			function isPostcode() {
				var postcode = document.getElementById("postcode").value;
				var isPostcode = /^[1-9][0-9]{5}$/;
				if(isPostcode.test(postcode)) {
					document.getElementById("postcodeShow").innerText = "验证通过";
					document.getElementById("postcodeShow").style.color = "green";
				} else {
					document.getElementById("postcodeShow").innerText = "输入格式错误!";
					document.getElementById("postcodeShow").style.color = "red";
				}
			}

银行卡号

//银行卡号
			function isBankCard() {
				var bankCard = document.getElementById("bankCard").value;
				var isBankCard = /^[6][0-9]{18}$/;
				if(isBankCard.test(bankCard)) {
					document.getElementById("bankCardShow").innerText = "验证通过";
					document.getElementById("bankCardShow").style.color = "green";
				} else {
					document.getElementById("bankCardShow").innerText = "输入格式错误!";
					document.getElementById("bankCardShow").style.color = "red";
				}
			}

学号

//学号
			function isStudentID() {
				var studentID = document.getElementById("studentID").value;
				var isStudentID = /^[0609]{4}((0[1-9])|(1[1-9])|(2[0]))[0-9]{4}$/;
				if(isStudentID.test(studentID)) {
					document.getElementById("studentIDShow").innerText = "验证通过";
					document.getElementById("studentIDShow").style.color = "green";
				} else {
					document.getElementById("studentIDShow").innerText = "输入格式错误!";
					document.getElementById("studentIDShow").style.color = "red";
				}
			}

车牌号

//车牌号
			function isCarNumber() {
				var carNumber = document.getElementById("carNumber").value;
				var isCarNumber = /^[\u4e00-\u9fa5][A-Z]\·[A-Z0-9]{5}$/;
				if(isCarNumber.test(carNumber)) {
					document.getElementById("carNumberShow").innerText = "验证通过";
					document.getElementById("carNumberShow").style.color = "green";
				} else {
					document.getElementById("carNumberShow").innerText = "输入格式错误!";
					document.getElementById("carNumberShow").style.color = "red";
				}
			}

快递单号

//快递单号
			function isCourierNumber() {
				var courierNumber = document.getElementById("courierNumber").value;
				var isCourierNumber = /^[JD]{2}[0-9]{13}$/;
				if(isCourierNumber.test(courierNumber)) {
					document.getElementById("courierNumberShow").innerText = "验证通过";
					document.getElementById("courierNumberShow").style.color = "green";
				} else {
					document.getElementById("courierNumberShow").innerText = "输入格式错误!";
					document.getElementById("courierNumberShow").style.color = "red";
				}
			}

验证码

//验证码
			function isCaptcha() {
				var captcha = document.getElementById("captcha").value;
				var isCaptcha = /^(([a-zA-Z0-9]{4})|([a-zA-Z0-9]{6}))$/;
				if(isCaptcha.test(captcha)) {
					document.getElementById("captchaShow").innerText = "验证通过";
					document.getElementById("captchaShow").style.color = "green";
				} else {
					document.getElementById("captchaShow").innerText = "输入格式错误!";
					document.getElementById("captchaShow").style.color = "red";
				}
			}

ISBN号

function isIsbn() {
				var isbn = document.getElementById("isbn").value;
				var isIsbn = /^((97[8])|(97[9]))-[0-9]-[0-9]{3}-[0-9]{5}-[0-9]$/; //978,979开头 后面横杠加数字978-x-xxx-xxxxx-x
				if(isIsbn.test(isbn)) {
					document.getElementById("isbnShow").innerText = "验证通过";
					document.getElementById("isbnShow").style.color = "green";
				} else {
					document.getElementById("isbnShow").innerText = "输入格式错误!";
					document.getElementById("isbnShow").style.color = "red";
				}
			}

网址

//网址
			function isWebsite() {
				var website = document.getElementById("website").value;
				var isWebsite = /^(http|https)(:\/\/)((www)\.)*[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)[/0-9a-zA-Z:\.?#=]*$/;
				if(isWebsite.test(website)) {
					document.getElementById("websiteShow").innerText = "验证通过";
					document.getElementById("websiteShow").style.color = "green";
				} else {
					document.getElementById("websiteShow").innerText = "输入格式错误!";
					document.getElementById("websiteShow").style.color = "red";
				}
			}

IPV4地址

//IPV4地址
			function isIpv4() {
				var ipv4 = document.getElementById("ipv4").value;
				var isIpv4 = /^((([0-9]{1,2})|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]{1,2})|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))$/;
				if(isIpv4.test(ipv4)) {
					document.getElementById("ipv4Show").innerText = "验证通过";
					document.getElementById("ipv4Show").style.color = "green";
				} else {
					document.getElementById("ipv4Show").innerText = "输入格式错误!";
					document.getElementById("ipv4Show").style.color = "red";
				}
			}

IPV6地址

//IPV6地址
			function isIpv6() {
				var ipv6 = document.getElementById("ipv6").value;
				var isIpv6 = /^(([a-fA-F0-9]{1,4}[:]){7}[a-fA-F0-9]{1,4})$/;
				if(isIpv6.test(ipv6)) {
					document.getElementById("ipv6Show").innerText = "验证通过";
					document.getElementById("ipv6Show").style.color = "green";
				} else {
					document.getElementById("ipv6Show").innerText = "输入格式错误!";
					document.getElementById("ipv6Show").style.color = "red";
				}
			}

出生年月日

//出生年月日
			function isBirthday() {
				var birthday = document.getElementById("birthday").value;
				var isBirthday = /^[0-9]{4}年((0[1-9])|(1[0-2]))月((0[1-9])|(1[0-9])|(2[0-9])|(3[0-1]))日$/;
				if(isBirthday.test(birthday)) {
					document.getElementById("birthdayShow").innerText = "验证通过";
					document.getElementById("birthdayShow").style.color = "green";
				} else {
					document.getElementById("birthdayShow").innerText = "输入格式错误!";
					document.getElementById("birthdayShow").style.color = "red";
				}
			}

姓名

//姓名
			function isName() {
				var name = document.getElementById("name").value;
				var isName = /^[\u4e00-\u9fa5]{2,}([·]*[\u4e00-\u9fa5])*$/;
				if(isName.test(name)) {
					document.getElementById("nameShow").innerText = "验证通过";
					document.getElementById("nameShow").style.color = "green";
				} else {
					document.getElementById("nameShow").innerText = "输入格式错误!";
					document.getElementById("nameShow").style.color = "red";
				}
			}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值