Js id method supports 15 and 18 digits

  • 2020-03-30 02:21:57
  • OfStack

 
//The HTML page should have an input field with id identity_card, a place where the id id ipmessage is incorrect or correct
<script> 
//Id card verification
$(document).ready(function(){ 
$("#identity_card").change(function(){ 
var idcard =$(this).val(); 
checkDate(idcard ); 
}); 
}); 
//Id card
function checkDate( idcard ){ 
var socialNo = idcard; 
if(socialNo == "") 
{ 
$('#ipmessage').html(' The format of input id number is incorrect , It must be 15 A to 18 Bit id number '); 
alert(" Input id number cannot be blank !"); 
return (false); 
} 
if (socialNo.length != 15 && socialNo.length != 18) 
{ 
$('#ipmessage').html(' The format of input id number is incorrect , It must be 15 A to 18 Bit id number '); 
alert(" The format of input id number is incorrect !"); 
return (false); 
} 
var area={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:" 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 "}; 
if(area[parseInt(socialNo.substr(0,2))]==null) { 
$('#ipmessage').html(' The format of input id number is incorrect , It must be 15 A to 18 Bit id number '); 
alert(" The id number is incorrect ( illicit )!"); 
return (false); 
} 
if (socialNo.length == 15) 
{ 
pattern= /^d{15}$/; 
if (pattern.exec(socialNo)==null){ 
$('#ipmessage').html(' The format of input id number is incorrect , It must be 15 A to 18 Bit id number '); 
alert("15 Bit id number must be number! "); 
return (false); 
} 
var birth = parseInt("19" + socialNo.substr(6,2)); 
var month = socialNo.substr(8,2); 
var day = parseInt(socialNo.substr(10,2)); 
switch(month) { 
case '01': 
case '03': 
case '05': 
case '07': 
case '08': 
case '10': 
case '12': 
if(day>31) { 
$('#ipmessage').html(' The format of input id number is incorrect , It must be 15 A to 18 Bit id number '); 
alert(' Input id number is not in the correct format !'); 
return false; 
} 
break; 
case '04': 
case '06': 
case '09': 
case '11': 
if(day>30) { 
$('#ipmessage').html(' The format of input id number is incorrect , It must be 15 A to 18 Bit id number '); 
alert(' Input id number is not in the correct format !'); 
return false; 
} 
break; 
case '02': 
if((birth % 4 == 0 && birth % 100 != 0) || birth % 400 == 0) { 
if(day>29) { 
$('#ipmessage').html(' The format of input id number is incorrect , It must be 15 A to 18 Bit id number '); 
alert(' Input id number is not in the correct format !'); 
return false; 
} 
} else { 
if(day>28) { 
$('#ipmessage').html(' The format of input id number is incorrect , It must be 15 A to 18 Bit id number '); 
alert(' Input id number is not in the correct format !'); 
return false; 
} 
} 
break; 
default: 
$('#ipmessage').html(' The format of input id number is incorrect , It must be 15 A to 18 Bit id number '); 
alert(' Input id number is not in the correct format !'); 
return false; 
} 
var nowYear = new Date().getYear(); 
if(nowYear - parseInt(birth)<15 || nowYear - parseInt(birth)>100) { 
$('#ipmessage').html(' The format of input id number is incorrect , It must be 15 A to 18 Bit id number '); 
alert(' Input id number is not in the correct format !'); 
return false; 
} 
$('#ipmessage').html(' Through! '); 
return (true); 
} 
var Wi = new Array( 
7,9,10,5,8,4,2,1,6, 
3,7,9,10,5,8,4,2,1 
); 
var lSum = 0; 
var nNum = 0; 
var nCheckSum = 0; 
for (i = 0; i < 17; ++i) 
{ 
if ( socialNo.charAt(i) < '0' || socialNo.charAt(i) > '9' ) 
{ 
$('#ipmessage').html(' The format of input id number is incorrect , It must be 15 A to 18 Bit id number '); 
alert(" The format of input id number is incorrect !"); 
return (false); 
} 
else 
{ 
nNum = socialNo.charAt(i) - '0'; 
} 
lSum += nNum * Wi[i]; 
} 
if( socialNo.charAt(17) == 'X' || socialNo.charAt(17) == 'x') 
{ 
lSum += 10*Wi[17]; 
} 
else if ( socialNo.charAt(17) < '0' || socialNo.charAt(17) > '9' ) 
{ 
$('#ipmessage').html(' The format of input id number is incorrect , It must be 15 A to 18 Bit id number '); 
alert(" The format of input id number is incorrect !"); 
return (false); 
} 
else 
{ 
lSum += ( socialNo.charAt(17) - '0' ) * Wi[17]; 
} 
if ( (lSum % 11) == 1 ) 
{ 
$('#ipmessage').html(' Through! '); 
return true; 
} 
else 
{ 
$('#ipmessage').html(' The format of input id number is incorrect , It must be 15 A to 18 Bit id number '); 
alert(" The format of input id number is incorrect !"); 
return (false); 
} 
} 
</script> 

Related articles: