Delphi写的验证身份证号有效性函数
来源:互联网 发布:网络远程教育报名流程 编辑:程序博客网 时间:2024/04/27 14:40
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;
{内部函数,取身份证号校验位,最后一位,对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;
- Delphi写的验证身份证号有效性函数
- Delphi写的验证身份证号有效性函数
- 绿色函数:验证身份证号有效性[转]
- js实现身份证号有效性验证
- iOS验证身份证号的有效性(仅18位号)
- js实现完美身份证号有效性验证
- js实现完美身份证号有效性验证
- js实现完美身份证号有效性验证
- js实现完美身份证号有效性验证
- js实现完美身份证号有效性验证
- PHP 验证身份证号是否正确的函数
- Oracle身份证号验证函数
- js实现完美身份证号有效性验证--身份证算法
- 身份证号的验证
- 推荐人身份证号的验证
- 身份证号的js验证
- 验证身份证号的合法性
- 数字证书的有效性验证
- VS2005 debug能运行release不能运行原因集
- H.264 数据分割
- 网络游戏外挂制作
- DVB-S DVB-T DVB-C
- 游戏外挂原理(转载)
- Delphi写的验证身份证号有效性函数
- C++编写木马全过程
- Linux RedHat Enterprise 的学习
- 一周的最后一天
- 我的第一个博客文章
- 在Fedora 11下安装NS2
- sigaction()
- 09年,发生了太多的事
- 遗言网站帮逝者继续交友