/** 中国公民身份证号码最小长度。 */
var CHINA_ID_MIN_LENGTH = 15;
/** 中国公民身份证号码最大长度。 */
var CHINA_ID_MAX_LENGTH = 18;
/** 每位加权因子 */
var power = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
/** 城市码 */
var cityCodes = {
"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": "国外"
};
/** 台湾身份首字母对应数字 */
var twFirstCode = {
"A": 10,
"B": 11,
"C": 12,
"D": 13,
"E": 14,
"F": 15,
"G": 16,
"H": 17,
"J": 18,
"K": 19,
"L": 20,
"M": 21,
"N": 22,
"P": 23,
"Q": 24,
"R": 25,
"S": 26,
"T": 27,
"U": 28,
"V": 29,
"X": 30,
"Y": 31,
"W": 32,
"Z": 33,
"I": 34,
"O": 35
};
/** 香港身份首字母对应数字 */
var hkFirstCode = {
"A": 1,
"B": 2,
"C": 3,
"R": 18,
"U": 21,
"Z": 26,
"X": 24,
"W": 23,
"O": 15,
"N": 14
};
/**
* 判断身份证号是否合法
*
* @param idCardNumber
* @return
*/
function isValidateIdCard(idCardNumber) {
var flag = false;
if (!idCardNumber){
return flag;
}
flag = validateCard(idCardNumber);
return flag;
}
/**
* 验证身份证是否合法
*/
function validateCard(idCard) {
var card = idCard.trim();
if (validateIdCard18(card)) {
return true;
}
if (validateIdCard15(card)) {
return true;
}
var cardval = validateIdCard10(card);
if (cardval != null) {
if (cardval[2] == "true") {
return true;
}
}
return false;
}
/**
* 验证18位身份编码是否合法
*
* @param idCard
* 身份编码
* @return 是否合法
*/
function validateIdCard18(idCard) {
var bTrue = false;
if (idCard.length == CHINA_ID_MAX_LENGTH) {
// 前17位
var code17 = idCard.substring(0, 17);
// 第18位
var code18 = idCard.substring(17, CHINA_ID_MAX_LENGTH);
if (!isNaN(parseInt(code17))) {
var cArr = code17.split("");
if (cArr != null) {
var iCard = cArr;
var iSum17 = getPowerSum(iCard);
// 获取校验位
var val = getCheckCode18(iSum17);
if (val.length > 0) {
if (val.toLowerCase() == code18.toLowerCase()) {
bTrue = true;
}
}
}
}
}
return bTrue;
}
/**
* 将身份证的每位和对应位的加权因子相乘之后,再得到和值
*
* @param iArr
* @return 身份证编码。
*/
function getPowerSum(iArr) {
var iSum = 0;
if (power.length == iArr.length) {
for (var i = 0; i < iArr.length; i++) {
for (var j = 0; j < power.length; j++) {
if (i == j) {
iSum = iSum + iArr[i] * power[j];
}
}
}
}
return iSum;
}
/**
* 将power和值与11取模获得余数进行校验码判断
*
* @param iSum
* @return 校验位
*/
function getCheckCode18(iSum) {
var sCode = "";
switch (iSum % 11) {
case 10:
sCode = "2";
break;
case 9:
sCode = "3";
break;
case 8:
sCode = "4";
break;
case 7:
sCode = "5";
break;
case 6:
sCode = "6";
break;
case 5:
sCode = "7";
break;
case 4:
sCode = "8";
break;
case 3:
sCode = "9";
break;
case 2:
sCode = "x";
break;
case 1:
sCode = "0";
break;
case 0:
sCode = "1";
break;
}
return sCode;
}
/**
* 验证15位身份编码是否合法
*
* @param idCard
* 身份编码
* @return 是否合法
*/
function validateIdCard15(idCard) {
if (idCard.length != CHINA_ID_MIN_LENGTH) {
return false;
}
if (!isNaN(idCard)) {
var proCode = idCard.substring(0, 2);
if (cityCodes.proCode == null) {
return false;
}
var birthCode = idCard.substring(6, 12);
var reg = /(^\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)$)/;
if (!reg.test(birthCode)) {
return false;
}
} else {
return false;
}
return true;
}
/**
* 验证10位身份编码是否合法
*
* @param idCard
* 身份编码
* @return 身份证信息数组
* <p>
* [0] - 台湾、澳门、香港 [1] - 性别(男M,女F,未知N) [2] - 是否合法(合法true,不合法false)
* 若不是身份证件号码则返回null
* </p>
*/
function validateIdCard10(idCard) {
var info = new Array(3);
var card = idCard.replace(/[\\(|\\)]/g, "");
if (card.length != 8 && card.length != 9 && idCard.length != 10) {
return null;
}
if (idCard.match("^[a-zA-Z][0-9]{9}$")) { // 台湾
info[0] = "台湾";
var char2 = idCard.substring(1, 2);
if (char2 == "1") {
info[1] = "M";
} else if (char2 == "2") {
info[1] = "F";
} else {
info[1] = "N";
info[2] = "false";
return info;
}
info[2] = validateTWCard(idCard) ? "true" : "false";
} else if (idCard.match("^[1|5|7][0-9]{6}\\(?[0-9A-Z]\\)?$")) { // 澳门
info[0] = "澳门";
info[1] = "N";
} else if (idCard.match("^[A-Z]{1,2}[0-9]{6}\\(?[0-9A]\\)?$")) { // 香港
info[0] = "香港";
info[1] = "N";
info[2] = validateHKCard(idCard) ? "true" : "false";
} else {
return null;
}
return info;
}
/**
* 验证台湾身份证号码
*
* @param idCard
* 身份证号码
* @return 验证码是否符合
*/
function validateTWCard(idCard) {
var start = idCard.substring(0, 1);
var mid = idCard.substring(1, 9);
var end = idCard.substring(9, 10);
var iStart = twFirstCode.start;
var sum = iStart / 10 + (iStart % 10) * 9;
var chars = mid.split("");
var iflag = 8;
for (var c in chars) {
sum = sum + parseInt(chars[c]) * iflag;
iflag--;
}
return (sum % 10 == 0 ? 0 : (10 - sum % 10)) == parseInt(end) ? true : false;
}
/**
* 验证香港身份证号码(存在Bug,部份特殊身份证无法检查)
* <p>
* 身份证前2位为英文字符,如果只出现一个英文字符则表示第一位是空格,对应数字58 前2位英文字符A-Z分别对应数字10-35
* 最后一位校验码为0-9的数字加上字符"A","A"代表10
* </p>
* <p>
* 将身份证号码全部转换为数字,分别对应乘9-1相加的总和,整除11则证件号码有效
* </p>
*
* @param idCard
* 身份证号码
* @return 验证码是否符合
*/
function validateHKCard(idCard) {
var card = idCard.replace(/[\\(|\\)]/g, "");
var sum = 0;
if (card.length == 9) {
sum = (parseInt(card.substring(0, 1).split("")[0]) - 55) * 9
+ (parseInt(card.substring(1, 2).split("")[0]) - 55) * 8;
card = card.substring(1, 9);
} else {
sum = 522 + (parseInt(card.substring(0, 1).split("")[0]) - 55) * 8;
}
var mid = card.substring(1, 7);
var end = card.substring(7, 8);
var chars = mid.split("");
var iflag = 7;
for (var c in chars) {
sum = sum + parseInt(chars[c]) * iflag;
iflag--;
}
if (end.toUpperCase() == ("A")) {
sum = sum + 10;
} else {
sum = sum + parseInt(end);
}
return (sum % 11 == 0) ? true : false;
}
javascript正则验证身份证号
最新推荐文章于 2024-07-17 11:23:05 发布