身份证验证


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 斌哥 阅读(119) 评论(0)  编辑 收藏 所属分类: Delphi编程技巧

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  博客园首页

  新闻频道

  社区

  小组

  博问

  网摘

  闪存

  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
成果网帮您增加网站收入


相关链接: