function GetIDInfoGetDInfo(const CardNo: string; var Sex,
BirthDay: string; var Age: Integer; var Area: string): Integer;
var
iCardNo: Int64;
iYear, iSex: Integer;
sBirth, fBirth: string;
dBirth: TDateTime;
begin
Result := 0; //表示身份证输入正确
if (Length(CardNo) <> 15) and (Length(CardNo) <> 18) then
begin
Result := 1; //位长不对
Exit;
end;
if Length(CardNo) = 15 then
begin
if not TryStrToInt64(CardNo, iCardNo) then
begin
Result := 2; //必须为数字
Exit;
end;
sBirth := '19' + Copy(CardNo, 7, 6);
iSex := StrToInt(Copy(CardNo, 15, 1));
if Odd(iSex) then
Sex := '男'
else
Sex := '女';
end
else
begin
if not TryStrToInt64(Copy(CardNo, 1, 17), iCardNo) then
begin
Result := 2; //前17位必须为数字
Exit;
end;
sBirth := Copy(CardNo, 7, 8);
iSex := StrToInt(Copy(CardNo, 17, 1));
case iSex of
0: Sex := '未知';
1: Sex := '男';
2: Sex := '女';
9: Sex := '未说明';
end;
end;
fBirth := Format('%s-%s-%s', [Copy(sBirth, 1, 4),
Copy(sBirth, 5, 2), Copy(sBirth, 7, 2)]);
if not TryStrToDate(fBirth, dBirth) then
begin
Result := 3; //生日格式不对
Exit;
end;
iYear := YearsBetween(Date, dBirth);
if (iYear < 0) or (iYear >= 120) then
begin
Result := 4; //年龄错误
Exit;
end;
BirthDay := sBirth;
Age := iYear;
Area := GetArea(StrToInt(Copy(CardNo, 1, 2)));
end;