绿色函数:验证身份证号有效性[转]

{验证身份证号有效性,返回值为空代表正常,否则为错误信息}
{作者:月夜风筝,edocu@163.com}
function ValidatePID(const APID: string): string;
{内部函数,取身份证号校验位,最后一位,对18位有效}
function GetVerifyBit(sIdentityNum: string): Char;
var
nNum: Integer;
begin
Result := #0;
nNum := StrToInt(sIdentityNum[1]) * 7 +
StrToInt(sIdentityNum[2]) * 9 +
StrToInt(sIdentityNum[3]) * 10 +
StrToInt(sIdentityNum[4]) * 5 +
StrToInt(sIdentityNum[5]) * 8 +
StrToInt(sIdentityNum[6]) * 4 +
StrToInt(sIdentityNum[7]) * 2 +
StrToInt(sIdentityNum[8]) * 1 +
StrToInt(sIdentityNum[9]) * 6 +
StrToInt(sIdentityNum[10]) * 3 +
StrToInt(sIdentityNum[11]) * 7 +
StrToInt(sIdentityNum[12]) * 9 +
StrToInt(sIdentityNum[13]) * 10 +
StrToInt(sIdentityNum[14]) * 5 +
StrToInt(sIdentityNum[15]) * 8 +
StrToInt(sIdentityNum[16]) * 4 +
StrToInt(sIdentityNum[17]) * 2;
nNum := nNum mod 11;
case nNum of
0: Result := '1';
1: Result := '0';
2: Result := 'X';
3: Result := '9';
4: Result := '8';
5: Result := '7';
6: Result := '6';
7: Result := '5';
8: Result := '4';
9: Result := '3';
10: Result := '2';
end;
end;
var
L : Integer;
sCentury : string;
sYear2Bit : string;
sMonth : string;
sDate : string;
iCentury : Integer;
iMonth : Integer;
iDate : Integer;
CRCFact : string;//18位证号的实际值
CRCTh : string; //18位证号的理论值
FebDayAmt: Byte;//2月天数
begin
L := Length(APID);
if (L in [15, 18]) = False then
begin
Result := Format('身份证号不是15位或18位(%0:s, 实际位数:%1:d)', [APID, L]);
Exit;
end;
CRCFact := '';
if L = 18 then
begin
sCentury := Copy(APID, 7, 2);
iCentury := StrToInt(sCentury);
if (iCentury in [18..20]) = False then
begin
Result := Format('身份证号码无效:18位证号的年份前两位必须在18-20之间(%0:S)', [sCentury]);
Exit;
end;
sYear2Bit := Copy(APID, 9, 2);
sMonth := Copy(APID, 11, 2);
sDate := Copy(APID, 13, 2);
CRCFact := Copy(APID, 18, 1);
end else
begin
sCentury := '19';
sYear2Bit := Copy(APID, 7, 2);
sMonth := Copy(APID, 9, 2);
sDate := Copy(APID, 11, 2);
end;
iMonth := StrToInt(sMonth);
iDate := StrToInt(sDate);
if (iMonth in [01..12]) = False then
begin
Result := Format('身份证号码无效:月份必须在01-12之间(%0:s)', [sMonth]);
Exit;
end;
if (iMonth in [1, 3, 5, 7, 8, 10, 12]) then
begin
if (iDate in [01..31]) = False then
begin
Result := Format('身份证号码无效:日期无效,不能为零或超出当月最大值(%0:s)', [sDate]);
Exit;
end;
end;
if (iMonth in [4, 6, 9, 11]) then
begin
if (iDate in [01..30]) = False then
begin
Result := Format('身份证号码无效:日期无效,不能为零或超出当月最大值(%0:s)', [sDate]);
Exit;
end;
end;
if IsLeapYear(StrToInt(sCentury + sYear2Bit)) = True then
begin
FebDayAmt := 29;
end else
begin
FebDayAmt := 28;
end;
if (iMonth in [2]) then
begin
if (iDate in [01..FebDayAmt]) = False then
begin
Result := Format('身份证号码无效:日期无效,不能为零或超出当月最大值(%0:s)', [sDate]);
Exit;
end;
end;
if CRCFact <> '' then
begin
CRCTh := GetVerifyBit(APID);
if CRCFact <> CRCTh then
begin
Result := Format('身份证号码无效:校验位(第18位)错:(%0:s)', [APID]);
Exit;
end;
end;
end; 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是验证身份证号码有效性的 JavaScript 代码: ``` function isValidID(id) { // 先判断身份证号码长度是否符合要求 if (id.length !== 18) { return false; } // 定义身份证号码前17位的权值 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 sum = 0; // 计算前17位的校验和 for (var i = 0; i < 17; i++) { sum += parseInt(id[i]) * weight[i]; } // 取校验和除以11的余数 var remainder = sum % 11; // 判断校验码是否符合要求 if (checkCode[remainder] !== id[17].toUpperCase()) { return false; } // 身份证号码校验通过 return true; } ``` 使用方法: ``` var id = '430426199803024336'; var result = isValidID(id); console.log(result); // 输出 true ``` ### 回答2: 下面是一个简单的用 JavaScript 编写的身份证有效性验证代码: ```javascript function validateIdCard(idCard) { if (!idCard || typeof idCard !== 'string') { return false; } // 验证身份证号长度 if (idCard.length !== 18) { return false; } // 验证前17位是否为数字 if (!/^\d{17}$/.test(idCard.substr(0, 17))) { return false; } // 验证最后一位校验码 var num = idCard.toUpperCase().split(''); var factors = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; var checkCodeMap = { '0': '1', '1': '0', '2': 'X', '3': '9', '4': '8', '5': '7', '6': '6', '7': '5', '8': '4', '9': '3', '10': '2' }; var sum = 0; for (var i = 0; i < 17; i++) { sum += num[i] * factors[i]; } var checkCode = sum % 11; if (checkCodeMap[checkCode] !== num[17]) { return false; } return true; } // 测试 var idCard1 = "110101199001011234"; console.log(validateIdCard(idCard1)); // true var idCard2 = "11010119900101123X"; console.log(validateIdCard(idCard2)); // true var idCard3 = "110101199001011235"; console.log(validateIdCard(idCard3)); // false ``` 上述代码实现了一个函数 `validateIdCard`,它接受一个身份证号作为参数。该函数首先进行一系列简单的校验,如长度和前17位是否为数字。然后使用权重因子和校验码对身份证号进行校验。校验结果为 `true` 表示身份证号有效,为 `false` 表示无效。 实际使用时,可以将身份证号作为参数传递给 `validateIdCard` 函数。你可以尝试不同的身份证号来进行测试。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值