这个实现的是点击下拉框选择身份证则验证身份证,点击护照则验证护照,那如何进行反选?就是点击文本框输入身份证号,那么下拉框只能选择身份证,文本框输入的是护照,点击下拉框只能选择护照 <script> $(document).ready(function () { $("#idType").change(function () { if ($("#idType option:selected").text()=="身份证"){ SelectCard(); }else if ($("#idType option:selected").text()=="护照"){ SelectpassPort(); } }); }); /*身份证验证*/ function SelectCard() { var inp = $('#idNbr'), //输入框 tips = $('#tips'); //提示文本框 inp.on({ keyup : function () { var val = $(this).val(); if(checkCard(val)){ tips.text('').css('color','green'); }else{ tips.text('请输入正确的身份证号').css('color','#f76d6d'); } }, }); function checkCard(num){ num = num.toUpperCase(); if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(num))) { return false; } var len, re; len = num.length; if (len == 15) { re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/); var arrSplit = num.match(re); var dtmBirth = new Date('19' + arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4]); var bGoodDay; bGoodDay = (dtmBirth.getYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4])); if (!bGoodDay) { return false; } else { var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); var nTemp = 0, i; num = num.substr(0, 6) + '19' + num.substr(6, num.length - 6); for (i = 0; i < 17; i ++) { nTemp += num.substr(i, 1) * arrInt[i]; } num += arrCh[nTemp % 11]; return true; } } if (len == 18) { re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/); var arrSplit = num.match(re); var dtmBirth = new Date(arrSplit[2] + "/" + arrSplit[3] + "/" + arrSplit[4]); var bGoodDay; bGoodDay = (dtmBirth.getFullYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4])); if (!bGoodDay) { return false; } else { var valnum; var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); var nTemp = 0, i; for(i = 0; i < 17; i ++) { nTemp += num.substr(i, 1) * arrInt[i]; } valnum = arrCh[nTemp % 11]; if (valnum != num.substr(17, 1)) { return false; } return true; } } return false; } } /*护照号验证*/ function SelectpassPort() { var inp = $('#idNbr'), //输入框 tips = $('#tips'); //提示文本框 inp.on({ keyup : function () { var val = $(this).val(); if(checkNumber(val)){ tips.text('').css('color','green'); }else{ tips.text('请输入正确的护照号').css('color','#f76d6d'); } }, }); function checkNumber(number){ var str=number; var Expression=/(P\d{7})|(G\d{8})/; var objExp=new RegExp(Expression); if(objExp.test(str)==true){ return true; }else{ return false; } } } </script>
慕瓜9220888
相关分类