<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script> </head> <body> <input type="text" class="" id="card"> <input type="submit" class="" id="sub"> <script> $(function () { $("#sub").click(function () { var val = $("#card").val(); var isidcardlen = val.length var reg15 = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/; var reg18 = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[A-Z])$/; if(isidcardlen == 15){/*15位身份证号*/ if(reg15.test(val)){ alert("15位身份证输入正确"); }else{ alert("15位身份证输入不正确"); } }else if(isidcardlen == 18){/*18位身份证号*/ if(reg18.test(val)){ alert("18位身份证输入正确"); }else{ alert("18位身份证输入不正确"); } }else{ alert("那你就输入错了"); } }) }) </script> </body> </html>
来源:CSDN
作者:New Learning
链接:https://blog.csdn.net/cheeso/article/details/79131638#comments