1. Simple regular expressions:
(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. More complex and strict validation:
//This can validate 15-digit and 18-digit ID cards, including birthday and check digit verification.
//If interested, you can also add verification of the ID card's origin area; the first 6 digits can be valid or invalid.
function isIdCardNo(num)
{
num = num.toUpperCase();
//ID card number is 15 or 18 digits. 15 digits are all numbers; 18 digits have first 17 as numbers, last as check digit which can be a number or X.
if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(num)))
{
alert('The ID card number length is incorrect or does not meet the requirements!\n15-digit numbers should be all numeric, 18-digit numbers can end with a digit or X.');
return false;
}
//Check digit is generated according to ISO 7064:1983.MOD 11-2, X can be considered as 10.
//Below analyze birth date and check digit respectively
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);
//Check if birth date is correct
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('The birth date in the ID card number is incorrect!');
return false;
}
else
{
//Convert 15-digit ID to 18-digit
//Check digit is generated according to ISO 7064:1983.MOD 11-2, X can be considered as 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);
//Check if birth date is correct
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('The birth date in the ID card number is incorrect!');
return false;
}
else
{
//Check if the check code of the 18-digit ID card is correct.
//Check digit is generated according to ISO 7064:1983.MOD 11-2, X can be considered as 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('The check digit of the 18-digit ID card is incorrect! It should be: ' + valnum);
return false;
}
return num;
}
}
return false;
}
3. Strict validation:
<script>
var aCity={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:'Macau',91:'Foreign'}
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: Illegal region';
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: Illegal birthday';
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: Illegal ID number';
return aCity[parseInt(sId.substr(0,2))]+','+sBirthday+','+(sId.substr(16,1)%2?'Male':'Female')
}
document.write(cidInfo("380524198002300016"),"<br/>");
document.write(cidInfo("340524198002300019"),"<br/>")
document.write(cidInfo("340524197711111111"),"<br/>")
document.write(cidInfo("34052419800101001x"),"<br/>");
</script>
暂无评论。