15 비트 와 18 비트 신분증 의 정규 표현 식 및 검증

5245 단어 정규 표현 식
1. 간단 한 정규 표현 식:
 
(1)preg_match("/^(\d{18,18}|\d{15,15}|\d{17,17}x)$/"
,$id_card) 

(2)preg_match("/^(\d{6})(18|19|20)?(\d{2})([01]\d)([0123]\d)(\d{3})(\d|X)?$/"
,$id_card) 

(3)preg_match("/(^\d{15}$/)|(\d{17}(?:\d|x|X)$/),$id_card)

 
2. 복잡 하고 엄격 한 검증:
//      15  18     ,             。   
//     ,              ,   6              。 

 
function isIdCardNo(num) 
{   
         num = num.toUpperCase();  

         //      15   18 ,15      ,18  17    ,        ,        X。   

          if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(num))){ 
             alert('           ,         !
15 ,18 X。'); return false; } // ISO 7064:1983.MOD 11-2 ,X 10。 // 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){ alert(' !'); return false; }else{ // 15 18 // ISO 7064:1983.MOD 11-2 ,X 10。 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 num; } } 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) { alert(dtmBirth.getYear()); alert(arrSplit[2]); alert(' !'); return false; } else { // 18 。 // ISO 7064:1983.MOD 11-2 ,X 10。 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)) { alert('18 ! :' + valnum); return false; } return num; } } return false; }

 
 
3. 엄격 한 검증 (18 비트 만):
<script>

var aCity={11:"  ",12:"  ",13:"  ",14:"  ",15:"   ",21:"  ",22:"  ",23:"   ",31:"  ",32:"  ",33:"  ",34:"  ",35:"  ",36:"  ",37:"  ",41:"  ",42:"  ",43:"  ",44:"  ",45:"  ",46:"  ",50:"  ",51:"  ",52:"  ",53:"  ",54:"  ",61:"  ",62:"  ",63:"  ",64:"  ",65:"  ",71:"  ",81:"  ",82:"  ",91:"  "}

 

function cidInfo(sId){

var iSum=0

var info=""

if(!/^d{17}(d|x)$/i.test(sId))return false;

sId=sId.replace(/x$/i,"a");

if(aCity[parseInt(sId.substr(0,2))]==null)return "Error:    ";

sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));

var d=new Date(sBirthday.replace(/-/g,"/"))

if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))return "Error:    ";

for(var i = 17;i>=0;i --) iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11)

if(iSum%11!=1)return "Error:    ";

return aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?" ":" ")

}document.write(cidInfo("380524198002300016"),"<br/>");

document.write(cidInfo("340524198002300019"),"<br/>")

document.write(cidInfo("340524197711111111"),"<br/>")

document.write(cidInfo("34052419800101001x"),"<br/>");

</script>


 

좋은 웹페이지 즐겨찾기