javascript verifies the validity of the id number and prompts

  • 2020-06-03 05:44:10
  • OfStack

javascript verifies the validity of the id number and prompts


function nunber(allowancePersonValue){ 
 if(allowancePersonValue==" Id number "){ 
 $("#span_username").show(); 
 $("#span_username").html(" The ID number cannot be empty "); 
 return false; 
 } 
 // Check length, type  
 else if(isCardNo(allowancePersonValue) === false) 
 { 
$("#span_username").show(); 
 $("#span_username").html(" The ID number you entered is not correct, please re-enter it "); 
 return false; 
 } 
 // Check the province  
 else if(checkProvince(allowancePersonValue) === false) 
 { 
 $("#span_username").show(); 
 $("#span_username").html(" The ID number you entered is not correct , Please re-enter "); 
 return false; 
 } 
 // Check the birthday  
 else if(checkBirthday(allowancePersonValue) === false) 
 { 
 $("#span_username").show(); 
 $("#span_username").html(" The id number you entered is incorrect , Please re-enter "); 
 return false; 
 } 
 // The detection of check bits  
 else if(checkParity(allowancePersonValue) === false) 
 { 
 $("#span_username").show(); 
 $("#span_username").html(" Your id check digit is incorrect , Please re-enter "); 
 return false; 
 }else{ 
 $("#span_username").hide(); 
 return true; 
 } 
 
} 
 
// Id province code  
var vcity={ 11:" Beijing ",12:" tianjin ",13:" hebei ",14:" shanxi ",15:" Inner Mongolia ", 
    21:" liaoning ",22:" Ji Lin ",23:" heilongjiang ",31:" Shanghai ",32:" jiangsu ", 
    33:" zhejiang ",34:" anhui ",35:" fujian ",36:" jiangxi ",37:" shandong ",41:" henan ", 
    42:" hubei ",43:" hunan ",44:" guangdong ",45:" guangxi ",46:" hainan ",50:" chongqing ", 
    51:"4 sichuan ",52:" guizhou ",53:" yunnan ",54:" Tibet ",61:" shaanxi ",62:" gansu ", 
    63:" qinghai ",64:" ningxia ",65:" xinjiang ",71:" Taiwan ",81:" Hong Kong ",82:" Macau ",91:" foreign " 
    }; 
 
// Check if the number conforms to the specification, including length, type  
function isCardNo(card){ 
 // Id card no. : 15 Bits or 18 position 15 The bits are all Numbers, 18 former 17 The digits are Numbers, and finally 1 A bit is a check bit and may be a number or character X 
 var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/; 
 if(reg.test(card) === false){ 
 //alert("demo"); 
 return false; 
 } 
 return true; 
} 
 
// Take the first two id CARDS , Check provinces  
function checkProvince(card){ 
 var province = card.substr(0,2); 
 if(vcity[province] == undefined){ 
 return false; 
 } 
 return true; 
} 
 
// Check if the birthday is correct  
function checkBirthday(card){ 
 var len = card.length; 
 // Id card 15 Bit, the order is province ( 3 (a) 3 A) in ( 2 (a) month 2 (a), 2 Bit) Check bit ( 3 Digits), are Numbers  
 if(len == '15'){ 
   var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/; 
   var arr_data = card.match(re_fifteen); 
   var year = arr_data[2]; 
   var month = arr_data[3]; 
   var day = arr_data[4]; 
   var birthday = new Date('19'+year+'/'+month+'/'+day); 
   return verifyBirthday('19'+year,month,day,birthday); 
 } 
 // Id card 18 Bit, the order is province ( 3 (a) 3 A) in ( 4 (a) month 2 (a), 2 Bit) Check bit ( 4 ), the end of the check bit may be X 
 if(len == '18'){ 
   var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/; 
   var arr_data = card.match(re_eighteen); 
   var year = arr_data[2]; 
   var month = arr_data[3]; 
   var day = arr_data[4]; 
   var birthday = new Date(year+'/'+month+'/'+day); 
   return verifyBirthday(year,month,day,birthday); 
 } 
 return false; 
} 
 
// Check the date  
function verifyBirthday(year,month,day,birthday){ 
 var now = new Date(); 
 var now_year = now.getFullYear(); 
 // Is the year month day reasonable  
 if(birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day) 
 { 
   // Range of Year ( 3 To the age of 100 Between the ages of ) 
   var time = now_year - year; 
   if(time >= 3 && time <= 100) 
   { 
     return true; 
   } 
   return false; 
 } 
 return false; 
} 
 
// Check bit detection  
function checkParity(card){ 
 //15 A turn 18 position  
 card = changeFivteenToEighteen(card); 
 var len = card.length; 
 if(len == '18'){ 
   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 cardTemp = 0, i, valnum; 
   for(i = 0; i < 17; i ++) 
   { 
     cardTemp += card.substr(i, 1) * arrInt[i]; 
   } 
   valnum = arrCh[cardTemp % 11]; 
   if (valnum == card.substr(17, 1)) 
   { 
     return true; 
   } 
   return false; 
 } 
 return false; 
} 
 
//15 A turn 18 Id number  
function changeFivteenToEighteen(card){ 
 if(card.length == '15') 
 { 
   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 cardTemp = 0, i;  
   card = card.substr(0, 6) + '19' + card.substr(6, card.length - 6); 
   for(i = 0; i < 17; i ++) 
   { 
     cardTemp += card.substr(i, 1) * arrInt[i]; 
   } 
   card += arrCh[cardTemp % 11]; 
   return card; 
 } 
 return card; 
} 

Usage:


<input type="text" class="reg_txt" value=" Id number " titles=" Please enter your ID number! " name="userid" id="username" maxlength="40" onfocus="if(this.value==' Id number '){this.value='';}" onblur="if(this.value==''){this.value=' Id number ';}nunber(this.value);" style="border: 1px solid red;"><br> 
<span class="c_red" id="span_username"></span> 

The effect is to enter the ID number, zoom out and remove the automatic verification, and return information!

This is the end of this article, I hope you enjoy it.


Related articles: