身份证验证


function IDCheck(e: string): Boolean;
var
  arrVerifyCode, Wi, Checker: TStringList;
  Ai: string;
  strYear, strMonth, strDay, BirthDay: string;
  i, TotalmulAiWi, modValue: Integer;
  strVerifyCode: string;
begin
  arrVerifyCode := SplitString('1,0,x,9,8,7,6,5,4,3,2', ',');

  Wi := SplitString('7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2', ',');
  Checker := SplitString('1,9,8,7,6,5,4,3,2,1,1', ',');
  if (Length(e) < 15) or (Length(e) = 17) or (Length(e) > 18) then
  begin
    result := False; //IDCheck= "身份证号共有 15 位或16位或18位"
    Exit;
  end;
  if (Length(e) = 16) and ((not IsNumerice(Copy(e, 16, 1))) and (LowerCase(Copy(e, 16, 1)) <> 'x')) then
  begin
    result := False; //IDCheck= "身份证号16位或最后一位为数值或X"
    Exit;
  end;
  if Length(e) = 18 then
  begin
    Ai := Copy(e, 1, 17);
  end
  else
  begin
    Ai := e; //15,16位身份证
    Ai := Copy(Ai, 1, 6) + '19' + Copy(Ai, 7, 9);
  end;
  if not IsNumerice(Ai) then
  begin
    Result := False; //IDCheck= "身份证除最后一位外,必须为数字!"
    Exit;
  end;
  strYear := Copy(Ai, 7, 4);
  strMonth := Copy(Ai, 11, 2);
  strDay := Copy(Ai, 13, 2);
  BirthDay := Trim(strYear) + '-' + Trim(strMonth) + '-' + Trim(strDay);
  if IsDate(BirthDay) then
  begin
//    ShowMessage(IntToStr( Now - StrToDate(BirthDay)));  1790 2007

    if (StrToInt(Copy(FormatDateTime('YYYY-MM-DD', Now), 1, 4)) - StrToInt(strYear) > 100) or (strtoDateTime(BirthDay) > date) then
    begin
      result := False; //IDCheck= "身份证输入错误!"  352625185012035477
      Exit;
    end;
  end
  else
  begin
    result := False; //IDCheck= "身份证输入错误!"
    Exit;
  end;

  for i := 0 to 16 do
  begin
    TotalmulAiWi := TotalmulAiWi + StrToInt(Copy(Ai, i + 1, 1)) * StrToInt(Wi[i]);
  end;
  modValue := TotalmulAiWi mod 11;
  strVerifyCode := arrVerifyCode[modValue];
  Ai := Ai + strVerifyCode;
  if (Length(e) = 18) and (e <> Ai) then
  begin
    Result := False; //IDCheck= "身份证号码输入错误!"
    Exit;
  end;
  Result := True;
end;

 

posted @ 2007-10-08 00:07  斌哥  阅读(422)  评论(0编辑  收藏  举报