delphi身份证验证

来源:互联网 发布:bim施工现场布置软件 编辑:程序博客网 时间:2024/05/18 03:14

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,身份证号码 (输入)
//返回值:sID是标准身份证号,返回OK,否则返回相应信息
function IsRightID(sID:string):Boolean;
  //判断是否存在非法字符
  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:=False;
  try
    if Length(sID)<>18 then
    begin
      ShowMessage('身份证号应为18位!');
      exit;
    end;
    if not JudgeIllegal(Copy(sID,1,17)) then
    begin
      ShowMessage('身份证号前17位应为数字!');
      exit;
    end;
    //判断成功
    if (Weighted(sID)=Copy(sID,18,1)) then
    begin


    end
    else
    begin
      ShowMessage('身份证号码有误!');
      Exit;
    end
  except
    on e:Exception do
    begin
      ShowMessage('异常:'+e.Message);
      Exit;
    end;
  end;
  Result := True;
end;
0 0
原创粉丝点击