The code copy is as follows:
//The HTML page should have an input input box with id identity_card, and a place to prompt a message if the ID card with id ipmessage is wrong or correct or is prompted.
<script>
//Identity 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 the input ID number is incorrect, it must be an ID number from 15 to 18 digits');
alert("Enter ID number cannot be empty!");
return (false);
}
if (socialNo.length != 15 && socialNo.length != 18)
{
$('#ipmessage').html('The format of the input ID number is incorrect, it must be an ID number from 15 to 18 digits');
alert("The format of the input ID number is incorrect!");
return (false);
}
var area={11:"Beijing",12:"Tianjin",13:"Hebei",14:"Shanxi",15:"Inner Mongolia",21:"Liaoning",22:"Jilin",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:"Macao",91:"Foreign"};
if(area[parseInt(socialNo.substr(0,2))]==null) {
$('#ipmessage').html('The format of the input ID number is incorrect, it must be an ID number from 15 to 18 digits');
alert("Improper ID number (illegal area)!");
return (false);
}
if (socialNo.length == 15)
{
pattern= /^/d{15}$/;
if (pattern.exec(socialNo)==null){
$('#ipmessage').html('The format of the input ID number is incorrect, it must be an ID number from 15 to 18 digits');
alert("15-digit ID number must be a 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 the input ID number is incorrect, it must be an ID number from 15 to 18 digits');
alert('The 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 the input ID number is incorrect, it must be an ID number from 15 to 18 digits');
alert('The 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 the input ID number is incorrect, it must be an ID number from 15 to 18 digits');
alert('The input ID number is not in the correct format!');
return false;
}
} else {
if(day>28) {
$('#ipmessage').html('The format of the input ID number is incorrect, it must be an ID number from 15 to 18 digits');
alert('The input ID number is not in the correct format!');
return false;
}
}
break;
default:
$('#ipmessage').html('The format of the input ID number is incorrect, it must be an ID number from 15 to 18 digits');
alert('The 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 the input ID number is incorrect, it must be an ID number from 15 to 18 digits');
alert('The input ID number is not in the correct format!');
return false;
}
$('#ipmessage').html('pass!');
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 the input ID number is incorrect, it must be an ID number from 15 to 18 digits');
alert("The format of the 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 the input ID number is incorrect, it must be an ID number from 15 to 18 digits');
alert("The format of the input ID number is incorrect!");
return (false);
}
else
{
lSum += ( socialNo.charAt(17) - '0' ) * Wi[17];
}
if ( (lSum % 11) == 1 )
{
$('#ipmessage').html('pass!');
return true;
}
else
{
$('#ipmessage').html('The format of the input ID number is incorrect, it must be an ID number from 15 to 18 digits');
alert("The format of the input ID number is incorrect!");
return (false);
}
}
</script>