Loading... <p>function ValidatePID(const APID: string): string;<br /> {内部函数,取身份证号校验位,最后一位,对18位有效}<br /> function GetVerifyBit(sIdentityNum: string): Char;<br /> var<br /> nNum: Integer;<br /> begin<br /> Result := #0;<br /> nNum := StrToInt(sIdentityNum[1]) * 7 +<br /> StrToInt(sIdentityNum[2]) * 9 +<br /> StrToInt(sIdentityNum[3]) * 10 + <br /> StrToInt(sIdentityNum[4]) * 5 + <br /> StrToInt(sIdentityNum[5]) * 8 +<br /> StrToInt(sIdentityNum[6]) * 4 +<br /> StrToInt(sIdentityNum[7]) * 2 +<br /> StrToInt(sIdentityNum[8]) * 1 +<br /> StrToInt(sIdentityNum[9]) * 6 +<br /> StrToInt(sIdentityNum[10]) * 3 +<br /> StrToInt(sIdentityNum[11]) * 7 +<br /> StrToInt(sIdentityNum[12]) * 9 +<br /> StrToInt(sIdentityNum[13]) * 10 +<br /> StrToInt(sIdentityNum[14]) * 5 +<br /> StrToInt(sIdentityNum[15]) * 8 +<br /> StrToInt(sIdentityNum[16]) * 4 +<br /> StrToInt(sIdentityNum[17]) * 2;<br /> nNum := nNum mod 11;<br /> case nNum of<br /> 0: Result := '1';<br /> 1: Result := '0';<br /> 2: Result := 'X';<br /> 3: Result := '9';<br /> 4: Result := '8';<br /> 5: Result := '7';<br /> 6: Result := '6';<br /> 7: Result := '5';<br /> 8: Result := '4';<br /> 9: Result := '3';<br /> 10: Result := '2';<br /> end;<br /> end;<br /> var<br /> L : Integer;<br /> sCentury : string;<br /> sYear2Bit : string;<br /> sMonth : string;<br /> sDate : string;<br /> iCentury : Integer;<br /> iMonth : Integer;<br /> iDate : Integer;<br /> CRCFact : string;//18位证号的实际值<br /> CRCTh : string; //18位证号的理论值<br /> FebDayAmt: Byte;//2月天数<br /> begin<br /> L := Length(APID);<br /> if (L in [15, 18]) = False then<br /> begin<br /> Result := Format('身份证号不是15位或18位(%0:s, 实际位数:%1:d)', [APID, L]);<br /> Exit;<br /> end;<br /> CRCFact := '';<br /> if L = 18 then<br /> begin<br /> sCentury := Copy(APID, 7, 2);<br /> iCentury := StrToInt(sCentury);<br /> if (iCentury in [18..20]) = False then<br /> begin<br /> Result := Format('身份证号码无效:18位证号的年份前两位必须在18-20之间(%0:S)', [sCentury]);<br /> Exit;<br /> end;<br /> sYear2Bit := Copy(APID, 9, 2);<br /> sMonth := Copy(APID, 11, 2);<br /> sDate := Copy(APID, 13, 2);<br /> CRCFact := Copy(APID, 18, 1);<br /> end else<br /> begin<br /> sCentury := '19';<br /> sYear2Bit := Copy(APID, 7, 2);<br /> sMonth := Copy(APID, 9, 2);<br /> sDate := Copy(APID, 11, 2);<br /> end;<br /> iMonth := StrToInt(sMonth);<br /> iDate := StrToInt(sDate);<br /> if (iMonth in [01..12]) = False then<br /> begin<br /> Result := Format('身份证号码无效:月份必须在01-12之间(%0:s)', [sMonth]);<br /> Exit;<br /> end;<br /> if (iMonth in [1, 3, 5, 7, 8, 10, 12]) then<br /> begin<br /> if (iDate in [01..31]) = False then<br /> begin<br /> Result := Format('身份证号码无效:日期无效,不能为零或超出当月最大值(%0:s)', [sDate]);<br /> Exit;<br /> end;<br /> end;<br /> if (iMonth in [4, 6, 9, 11]) then<br /> begin<br /> if (iDate in [01..30]) = False then<br /> begin<br /> Result := Format('身份证号码无效:日期无效,不能为零或超出当月最大值(%0:s)', [sDate]);<br /> Exit;<br /> end;<br /> end;<br /> if IsLeapYear(StrToInt(sCentury + sYear2Bit)) = True then<br /> begin<br /> FebDayAmt := 29;<br /> end else<br /> begin<br /> FebDayAmt := 28;<br /> end;<br /> if (iMonth in [2]) then<br /> begin<br /> if (iDate in [01..FebDayAmt]) = False then<br /> begin<br /> Result := Format('身份证号码无效:日期无效,不能为零或超出当月最大值(%0:s)', [sDate]);<br /> Exit;<br /> end;<br /> end;<br /> if CRCFact <> '' then<br /> begin<br /> CRCTh := GetVerifyBit(APID);<br /> if CRCFact <> CRCTh then<br /> begin<br /> Result := Format('身份证号码无效:校验位(第18位)错:(%0:s)', [APID]);<br /> Exit;<br /> end;<br /> end;<br /> end;</p> <p>本文来自Delphi之窗,原文地址:<a href="https://www.samool.com/go/aHR0cDovL3d3dy41MmRlbHBoaS5jb20=" target="_blank" >http://www.52delphi.com</a><br /> </p> 相关文章 无相关文章 Last modification:February 16th, 2010 at 09:30 pm © 允许规范转载 Support 如果觉得我的文章对你有用,请随意赞赏 ×Close Appreciate the author Sweeping payments Pay by AliPay Pay by WeChat