正则表达式.

<!--QQ号-->   
<!--正则表达式-->
<!--var qq = /^[1-9][0-9]{4,9}$/;  //第一位不能为0,最少5位,最多10位-->

<!--手机号-->
<!--正则表达式-->
<!--var phoneNumber = /^1[3-9][0-9]\d{9}$/;-->

<!--邮箱 XXXX@XXXX.XXXX-->
<!--正则表达式-->
<!--var email = /^[A-Za-z0-9]+@[A-Za-z]+(\.\w{2,})+$/;-->

<!--地址 XX省-XX市-XX县-->
<!--正则表达式-->
<!--// 地址 有中文 有数字 有横杠- -->
<!--var address = /^[\u4e00-\u9fa5]+[\d|-]+/;-->

<!--邮编-->
<!--正则表达式-->
<!--var postCode = /^[1-9]\d{5}$/;-->

<!--银行卡号-->
<!--正则表达式-->
<!--var bankCardNum = /^[1-9][0-9]{18}$/;-->

<!--学号-->
<!--正则表达式-->
<!--var stuNum = /^[0-9]{10}$/;-->

<!--车牌号 豫A56789-->
<!--正则表达式-->
<!--//省份简称(1位汉字)+发牌机关代号(1位字母)+序号(6位)-->
<!--//总计8个字符,序号不能出现-->
<!--var carNum = /^[\u4E00-\u9FA5][A-Z][A-Z0-9]{5}$/;-->

<!--快递单号(以韵达为例)-->
<!--正则表达式-->
<!--var pressNum = /^[\s]*[0-9]{13}[\s]*$/;-->

<!--ISBN号 978-X-XXXX-XXXX-X-->
<!--正则表达式-->
<!--//共13位 978为固定 其他十位 第一位为中国的标识7 之后是标识出版社的2-6位数字-->
<!--// 之后是出版社内刊物的流水 最后一位是校验位0-9和X-->
<!--var ISBN = /^978-[0-9]-[0-9]{4}[-][0-9]{4}-[0-9X]$/;-->

<!--网址(www.baidu.com)-->
<!--正则表达式-->
<!--var webSite =/^[a-zA-Z]+[\.](\D|\d)+[\.]\D+$/;-->

<!--IPV4地址-->
<!--正则表达式-->
<!--var IPV4 =/((([0-9]{1,2})|(1[0-9]{1,2})|2([0-4][0-9])|(25[0-5]))\.){3}(([0-9]{1,2})|(1[0-9]{1,2})|2([0-4][0-9])|(25[0-5]))/;-->

<!--IPV6地址-->
<!--正则表达式-->
<!--var IPV6 =/(([A-Fa-f0-9]{1,4}:){1,7}([A-Fa-f0-9]{1,4}))/;-->

<!--出生年月日(XXXX年XX月XX日)-->
<!--正则表达式-->
<!--var birthday = /^[0-9]{4}年(([0][1-9])|([1][1-2]))月((0[1-9])|(1[0-9])|(2[0-9])|(3[0-1]))日$/;-->

<!--姓名(中文·中文)-->
<!--正则表达式-->
<!--var name =/^[\u4e00-\u9fa5]{2,}([·]*[\u4e00-\u9fa5]*)$/;;-->

代码示例:

正则表达式
var qq = /^[1-9][0-9]{4,9}$/;  //第一位不能为0,最少5位,最多10位

完整代码
<input type="text" id="qqInput" placeholder="请输入QQ号" onblur="qqTest()">
<span id="qqShow"></span><br/>
<script>
// QQ号
function qqTest() {
    var qq = /^[1-9][0-9]{4,9}$/;
    var qqInput = document.getElementById("qqInput").value;
    var qqShowDao = document.getElementById("qqShow");
    if (qq.test(qqInput)) {
        qqShowDao.innerText = "格式正确!";
        qqShowDao.style.color = "green";
    } else {
        qqShowDao.innerText = "格式错误!!!";
        qqShowDao.style.color = "red";
    }
}
</script>

手机号
正则表达式
var phoneNumber = /^1[3-9][0-9]\d{9}$/;

完整代码
<input type="text" id="phoneNumberInput" placeholder="请输入手机号" onblur="phoneNumberTest()">
<span id="phoneNumberShow"></span><br/>
<script>
// 手机号
function phoneNumberTest() {
    var phoneNumber = /^1[3-9][0-9]\d{9}$/;
    var phoneNumberInput = document.getElementById("phoneNumberInput").value;
    var phoneNumberShowDao = document.getElementById("phoneNumberShow");
    if (phoneNumber.test(phoneNumberInput)) {
        phoneNumberShowDao.innerText = "格式正确!";
        phoneNumberShowDao.style.color = "green";
    } else {
        phoneNumberShowDao.innerText = "格式错误!!!";
        phoneNumberShowDao.style.color = "red";
    }
}
</script>

邮箱 XXXX@XXXX.XXXX
正则表达式
var email = /^[A-Za-z0-9]+@[A-Za-z]+(\.\w{2,})+$/;

完整代码
<input type="text" id="emailInput" placeholder="请输入邮箱号" onblur="emailTest()">
<span id="emailShow"></span><br/>
<script>
// 邮箱
function emailTest() {
    // \w单字字符 等价[A-Za-z0-9]
    var email = /^[A-Za-z0-9]+@[A-Za-z]+(\.\w{2,})+$/;
    var eamilInput = document.getElementById("emailInput").value;
    var emailShowDao = document.getElementById("emailShow");
    if (email.test(eamilInput)) {
        emailShowDao.innerText = "格式正确!";
        emailShowDao.style.color = "green";
    } else {
        emailShowDao.innerText = "格式错误!!!";
        emailShowDao.style.color = "red";
    }
}
</script>

地址 XX省-XX市-XX县
正则表达式
// 地址 有中文 有数字 有横杠-
var address = /^[\u4e00-\u9fa5]+[\d|-]+/;

完整代码
<input type="text" id="addressInput" placeholder="请输入地址" onblur="addressTest()">
<span id="addressShow"></span><br/>
<script>
// 地址 有中文 有数字 有横杠-
function addressTest() {
    var address = /^[\u4e00-\u9fa5]+[\d|-]+/;
    var addressInput = document.getElementById("addressInput").value;
    var addressShowDao = document.getElementById("addressShow");
    if (address.test(addressInput)) {
        addressShowDao.innerText = "格式通过!";
        addressShowDao.style.color = "green";
    } else {
        addressShowDao.innerText = "格式错误!!!";
        addressShowDao.style.color = "red";
    }
}
</script>

邮编
正则表达式
var postCode = /^[1-9]\d{5}$/;

完整代码
<input type="text" id="postCodeInput" placeholder="请输入邮编" onblur="postCodeTest()">
<span id="postCodeShow"></span><br/>
<script>
// 邮编
function postCodeTest() {
    var postCode = /^[1-9]\d{5}$/;
    var postCodeInput = document.getElementById("postCodeInput").value;
    var postCodeShowDao = document.getElementById("postCodeShow");
    if (postCode.test(postCodeInput)) {
        postCodeShowDao.innerText = "格式正确!";
        postCodeShowDao.style.color = "green";
    } else {
        postCodeShowDao.innerText = "格式错误!!!";
        postCodeShowDao.style.color = "red";
    }
}
</script>

银行卡号
正则表达式
var bankCardNum = /^[1-9][0-9]{18}$/;

完整代码
<input type="text" id="bankCardNumInput" placeholder="请输入银行卡号" onblur="bankCardNumTest()">
<span id="bankCardNumShow"></span><br/>
<script>
function bankCardNumTest() {
   var bankCardNum = /^[1-9][0-9]{18}$/;
   var bankCardNumInput = document.getElementById("bankCardNumInput").value;
   var bankCardNumShowDao = document.getElementById("bankCardNumShow");
   if (bankCardNum.test(bankCardNumInput)) {
       bankCardNumShowDao.innerText = "格式正确!";
       bankCardNumShowDao.style.color = "green";
   } else {
       bankCardNumShowDao.innerText = "格式错误!!!";
       bankCardNumShowDao.style.color = "red";
   }
}
</script>

学号
正则表达式
var stuNum = /^[0-9]{10}$/;

完整代码
<input type="text" id="stuNumInput" placeholder="请输入学号" onblur="stuNumTest()">
<span id="stuNumShow"></span><br/>
<script>
// 学号
function stuNumTest() {
    var stuNum = /^[0-9]{10}$/;
    var stuNumInput = document.getElementById("stuNumInput").value;
    var stuNumShowDao = document.getElementById("stuNumShow");
    if (stuNum.test(stuNumInput)) {
        stuNumShowDao.innerText = "格式正确!";
        stuNumShowDao.style.color = "green";
    } else {
        stuNumShowDao.innerText = "格式错误!!!";
        stuNumShowDao.style.color = "red";
    }
}
</script>

车牌号 豫A56789
正则表达式
//省份简称(1位汉字)+发牌机关代号(1位字母)+序号(6位)
//总计8个字符,序号不能出现
var carNum = /^[\u4E00-\u9FA5][A-Z][A-Z0-9]{5}$/;

完整代码
<input type="text" id="carNumInput" placeholder="请输入车牌号" onblur="carNumTest()">
<span id="carNumShow"></span><br/>
<script>
//车牌号
//省份简称(1位汉字)+发牌机关代号(1位字母)+序号(6位),总计8个字符,序号不能出现
function carNumTest() {
    var carNum = /^[\u4E00-\u9FA5][A-Z][A-Z0-9]{5}$/;
    var carNumInput = document.getElementById("carNumInput").value;
    var carNumShowDao = document.getElementById("carNumShow");
    if (carNum.test(carNumInput)) {
        carNumShowDao.innerText = "格式正确!";
        carNumShowDao.style.color = "green";
    } else {
        carNumShowDao.innerText = "格式错误!!!";
        carNumShowDao.style.color = "red";
    }
}
</script>

快递单号(以韵达为例)
正则表达式
var pressNum = /^[\s]*[0-9]{13}[\s]*$/;

完整代码
<input type="text" id="pressNumInput" placeholder="请输入快递单号" onblur="pressNumTest()">
<span id="pressNumShow"></span><br/>
<script>
//快递单号,以韵达为例
function pressNumTest() {
    var pressNum = /^[\s]*[0-9]{13}[\s]*$/;
    var pressNumInput = document.getElementById("pressNumInput").value;
    var pressNumShowDao = document.getElementById("pressNumShow");
    if (pressNum.test(pressNumInput)) {
        pressNumShowDao.innerText = "格式正确!";
        pressNumShowDao.style.color = "green";
    } else {
        pressNumShowDao.innerText = "格式错误!!!";
        pressNumShowDao.style.color = "red";
    }
}
</script>

验证码(四位或六位)
正则表达式
var verification = /^(\w{4})|(\w{6})$/;

完整代码
<input type="text" id="verificationInput" placeholder="请输入验证码" onblur="verificationTest()">
<span id="verificationShow"></span><br/>
<script>
//快递单号
function verificationTest() {
    var verification = /^(\w{4})|(\w{6})$/;
    var verificationInput = document.getElementById("verificationInput").value;
    var verificationShowDao = document.getElementById("verificationShow");
    if (verification.test(verificationInput)) {
        verificationShowDao.innerText = "格式正确!";
        verificationShowDao.style.color = "green";
    } else {
        verificationShowDao.innerText = "格式错误!!!";
        verificationShowDao.style.color = "red";
    }
}
 </script>

ISBN号 978-X-XXXX-XXXX-X
正则表达式
//共13位 978为固定 其他十位 第一位为中国的标识7 之后是标识出版社的2-6位数字
// 之后是出版社内刊物的流水 最后一位是校验位0-9和X
var ISBN = /^978-[0-9]-[0-9]{4}[-][0-9]{4}-[0-9X]$/;

完整代码
<input type="text" id="ISBNInput" placeholder="请输入ISBN号" onblur="ISBNTest()">
span id="ISBNShow"></span><br/>
 //ISBN号
//共13位 978为固定 其他十位 第一位为中国的标识7 之后是标识出版社的2-6位数字
// 之后是出版社内刊物的流水 最后一位是校验位0-9和X

<script>
function ISBNTest() {
    var ISBN = /^978-[0-9]-[0-9]{4}[-][0-9]{4}-[0-9X]$/;
    var ISBNInput = document.getElementById("ISBNInput").value;
    var ISBNShowDao = document.getElementById("ISBNShow");
    if (ISBN.test(ISBNInput)) {
        ISBNShowDao.innerText = "格式正确!";
        ISBNShowDao.style.color = "green";
    } else {
        ISBNShowDao.innerText = "格式错误!!!";
        ISBNShowDao.style.color = "red";
    }
}
</script>

网址(www.baidu.com)
正则表达式
var webSite =/^[a-zA-Z]+[\.](\D|\d)+[\.]\D+$/;

完整代码
<input type="text" id="webSiteInput" placeholder="请输入网址" onblur="webSiteTest()">
<span id="webSiteShow"></span><br/>
<script>
function webSiteTest() {
    var webSite = /^[a-zA-Z]+[\.](\D|\d)+[\.]\D+$/;
    var webSiteInput = document.getElementById("webSiteInput").value;
    var webSiteShowDao = document.getElementById("webSiteShow");
    if (webSite.test(webSiteInput)) {
        webSiteShowDao.innerText = "格式正确!";
        webSiteShowDao.style.color = "green";
    } else {
        webSiteShowDao.innerText = "格式错误!!!";
        webSiteShowDao.style.color = "red";
    }
}
</script>

IPV4地址
正则表达式
var IPV4 =/((([0-9]{1,2})|(1[0-9]{1,2})|2([0-4][0-9])|(25[0-5]))\.){3}(([0-9]{1,2})|(1[0-9]{1,2})|2([0-4][0-9])|(25[0-5]))/;

完整代码
<input type="text" id="IPV4Input" placeholder="请输入IPV4地址"  onblur="IPV4Test()">
<span id="IPV4Show"></span><br/>
<script>
function IPV4Test() {
    var IPV4 =
        /((([0-9]{1,2})|(1[0-9]{1,2})|2([0-4][0-9])|(25[0-5]))\.){3}(([0-9]{1,2})|(1[0-9]{1,2})|2([0-4][0-9])|(25[0-5]))/;
    var IPV4Input = document.getElementById("IPV4Input").value;
    var IPV4ShowDao = document.getElementById("IPV4Show");
    if (IPV4.test(IPV4Input)) {
        IPV4ShowDao.innerText = "格式正确!";
        IPV4ShowDao.style.color = "green";
    } else {
        IPV4ShowDao.innerText = "格式错误!!!";
        IPV4ShowDao.style.color = "red";
    }
}
</script>

<IPV6地址
正则表达式
var IPV6 =/(([A-Fa-f0-9]{1,4}:){1,7}([A-Fa-f0-9]{1,4}))/;

完整代码
<input type="text" id="IPV6Input" placeholder="请输入IPV6地址"  onblur="IPV6Test()">
<span id="IPV6Show"></span><br/>
<script>
function IPV6Test() {
    var IPV6 = /(([A-Fa-f0-9]{1,4}:){1,7}([A-Fa-f0-9]{1,4}))/;
    var IPV6Input = document.getElementById("IPV6Input").value;
    var IPV6ShowDao = document.getElementById("IPV6Show");
    if (IPV6.test(IPV6Input)) {
        IPV6ShowDao.innerText = "格式正确!";
        IPV6ShowDao.style.color = "green";
    } else {
        IPV6ShowDao.innerText = "格式错误!!!";
        IPV6ShowDao.style.color = "red";
    }
}
</script>

 出生年月日(XXXX年XX月XX日)
正则表达式
var birthday = /^[0-9]{4}年(([0][1-9])|([1][1-2]))月((0[1-9])|(1[0-9])|(2[0-9])|(3[0-1]))日$/;

完整代码
<input type="text" id="birthdayInput" placeholder="请输入出生年月" onblur="birthdayTest()">
<span id="birthdayShow"></span><br/>
<script>
function birthdayTest() {
    var birthday = /^[0-9]{4}年(([0][1-9])|([1][1-2]))月((0[1-9])|(1[0-9])|(2[0-9])|(3[0-1]))日$/;
    var birthdayInput = document.getElementById("birthdayInput").value;
    var birthdayShowDao = document.getElementById("birthdayShow");
    if (birthday.test(birthdayInput)) {
        birthdayShowDao.innerText = "格式正确!";
        birthdayShowDao.style.color = "green";
    } else {
        birthdayShowDao.innerText = "格式错误!!!";
        birthdayShowDao.style.color = "red";
    }
}
</script>

姓名(中文·中文)
正则表达式
var name =/^[\u4e00-\u9fa5]{2,}([·]*[\u4e00-\u9fa5]*)$/;;

完整代码
<input type="text" id="nameInput" placeholder="请输入姓名" onblur="nameTest()">
<span id="nameShow"></span><br/>
<script>
function nameTest() {
   var name =/^[\u4e00-\u9fa5]{2,}([·]*[\u4e00-\u9fa5]*)$/;;
   var nameInput = document.getElementById("nameInput").value;
   var nameShowDao = document.getElementById("nameShow");
   if (name.test(nameInput)) {
       nameShowDao.innerText = "格式正确!";
       nameShowDao.style.color = "green";
   } else {
       nameShowDao.innerText = "格式错误!!!";
       nameShowDao.style.color = "red";
   }
}
 </script>
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页