uses DateUtils;
const
IntMultiplication: Array[1..17] Of Integer=(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2);
//函数名:IsRightID
//参数:sID,身份证号码 (输入)
// sSex,性别 (输出)
// age,年龄 (输出)
//返回值:sID是标准身份证号,返回OK,否则返回相应信息
function IsRightID(sID:string;var sSex:string;var age:Integer):string;
//判断是否存在非法字符
function JudgeIllegal(str:string):Boolean;
var
i:Integer;
begin
Result:=False;
for i := 1 to Length(str) do
begin
case str[i] of ‘0‘..‘9‘:
begin
end
else
begin
Exit;
end;
end;
end;
Result := True;
end;
//加权函数
function Weighted(str:string):string;
var
i:Integer;
sum:Integer;
begin
sum:=0;
for i:=1 to 17 do
begin
sum:=sum+StrToInt(str[i])*IntMultiplication[i];
end;
sum:=sum mod 11;
case sum 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;
begin
Result:=‘‘;
try
if Length(sID)<>18 then
begin
Result:=‘身份证号应为18位!‘;
exit;
end;
if not JudgeIllegal(Copy(sID,1,17)) then
begin
Result:=‘身份证号前17位应为数字!‘;
exit;
end;
//判断成功
if (Weighted(sID)=Copy(sID,18,1)) then
begin
Result:=‘OK‘;
//判断性别
if (StrToInt(Copy(sID,17,1)) mod 2)=0 then
sSex:=‘女‘
else
sSex:=‘男‘;
//计算年龄
age:=Yearof(Now)-StrToInt(Copy(sID,7,4));
end
else
begin
Result:=‘身份证号码有误!‘;
end
except
on e:Exception do
begin
Result:=‘异常:‘+e.Message;
end;
end;
end;