组织机构代码
function verifyOrganization(code) {
if (code == null || code == '' || code.length != 9) {
return false;
}
code = code.toUpperCase();
var codeArray = code.split('');
var comparison = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; //代码值对照表
var weight = [3, 7, 9, 10, 5, 8, 4, 2]; //权值数组
var sum = 0; //初始化总和
for (var i = 0; i < 8; i++) {
sum += parseInt(comparison.indexOf(codeArray[i])) * weight[i];
}
var checkBit = 11 - (sum % 11); //计算校验位
if (checkBit == 11) {
checkBit = 0;
} else if (checkBit == 10) {
checkBit = 'X';
}
if (codeArray[8] == checkBit) { //校验位通过
return true;
} else { //校验位不通过
return false;
}
}
验证统一社会信用代码
function verifyUnite(code) {
if (code == null || code == '' || code.length != 18) {
return false;
}
code = code.toUpperCase();
var codeArray1 = code.split('');
var comparison1 = '0123456789ABCDEFGHJKLMNPQRTUWXY'; //代码值对照表
var weight1 = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28]; //权值数组
var sum1 = 0; //初始化总和
for (var j = 0; j < 17; j++) { //计算第一到十七位
sum1 += parseInt(comparison1.indexOf(codeArray1[j])) * weight1[j];
}
var checkBit1 = 31 - (sum1 % 31);
if (checkBit1 == 31) {
checkBit1 = 0;
}
if (comparison1.substr(checkBit1, 1) == codeArray1[17]) { //校验位通过
return true;
} else { //校验位不通过
return false;
}
}
验证身份证号
function verifyIdcard(value) { //验证身份证号
idCard = value;
if (idCard.length != 18) {
return false;
}
idCard = idCard.toUpperCase();
var weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
var checkCode = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
var idCardArray = idCard.split("");
var sum = 0;
//验证校验位
for (var i = 0; i < 17; i++) {
sum += parseInt(idCard[i]) * weight[i];
}
if (idCard[17] == checkCode[sum % 11]) { //判断校验码位
return true;
} else { //校验位验证不通过
return false;
}
}