Delphi:校正手機號及社會安全號碼

來源:互聯網
上載者:User

標籤:

//校正手機號  
function
  IsMobileNumber( num: string  ): boolean ;    begin      Result:= False ;      if  length( trim( Num ) ) <>  11  then  Exit;      if  ( ( copy( num,  1 2 ) <>  ‘13‘  and  ( copy( num ,  1 2 ) <>  ‘15‘  ) )  then  Exit;      try        StrToInt( copy( num,  3 9  ) );        Result:= True ;      except      end ;    end ;

//校正身份證,合法返回字元1,否則返回格式錯誤資訊
function ValidatePID(const APID: string): string;
  {內建函式,取社會安全號碼校正位,最後一位,對18位有效}
  function GetVerifyBit(sIdentityNum: string): Char;
  var
    nNum: Integer;
  begin
    Result := #0;
    nNum := StrToInt(sIdentityNum[1]) * 7
      StrToInt(sIdentityNum[2]) * 9
      StrToInt(sIdentityNum[3]) * 10
      StrToInt(sIdentityNum[4]) * 5
      StrToInt(sIdentityNum[5]) * 8
      StrToInt(sIdentityNum[6]) * 4
      StrToInt(sIdentityNum[7]) * 2
      StrToInt(sIdentityNum[8]) * 1
      StrToInt(sIdentityNum[9]) * 6
      StrToInt(sIdentityNum[10]) * 3
      StrToInt(sIdentityNum[11]) * 7
      StrToInt(sIdentityNum[12]) * 9
      StrToInt(sIdentityNum[13]) * 10
      StrToInt(sIdentityNum[14]) * 5
      StrToInt(sIdentityNum[15]) * 8
      StrToInt(sIdentityNum[16]) * 4
      StrToInt(sIdentityNum[17]) * 2;
    nNum := nNum mod 11;
    case nNum 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;
var
  L: Integer;
  sCentury: string;
  sYear2Bit, sYear4Bit: string;
  sMonth: string;
  sDate: string;
  iCentury: Integer;
  iMonth: Integer;
  iDate: Integer;
  CRCFact: string; //18位證號的實際值
  CRCTh: string; //18位證號的理論值
  FebDayAmt: Byte; //2月天數
begin
  L := Length(APID);
  if (L in [15, 18]) = False then
  begin
    Result := Format(‘社會安全號碼不是15位或18位(%0:s, 實際位元:%1:d)‘, [APID, L]);
    Exit;
  end;
  CRCFact := ‘‘;
  if L = 18 then
  begin
    sCentury := Copy(APID, 7, 2);
    iCentury := StrToInt(sCentury);
    if (iCentury in [18..20]) = False then
    begin
      Result := Format(‘社會安全號碼碼無效:18位證號的年份前兩位必須在18-20之間(%0:S)‘, [sCentury]);
      Exit;
    end;
    sYear2Bit := Copy(APID, 9, 2);
    sYear4Bit := sCentury sYear2Bit;
    sMonth := Copy(APID, 11, 2);
    sDate := Copy(APID, 13, 2);
    CRCFact := Copy(APID, 18, 1);
  end else
  begin
    sCentury := ‘19‘;
    sYear2Bit := Copy(APID, 7, 2);
    sYear4Bit := sCentury sYear2Bit;
    sMonth := Copy(APID, 9, 2);
    sDate := Copy(APID, 11, 2);
  end;
  iMonth := StrToInt(sMonth);
  iDate := StrToInt(sDate);
  if (iMonth in [01..12]) = False then
  begin
    Result := Format(‘社會安全號碼碼無效:月份必須在01-12之間(%0:s)‘, [sMonth]);
    Exit;
  end;
  if (iMonth in [1, 3, 5, 7, 8, 10, 12]) then
  begin
    if (iDate in [01..31]) = False then
    begin
      Result := Format(‘社會安全號碼碼無效:日期無效,不能為零或超出當月最大值(%0:s)‘, [sDate]);
      Exit;
    end;
  end;
  if (iMonth in [4, 6, 9, 11]) then
  begin
    if (iDate in [01..30]) = False then
    begin
      Result := Format(‘社會安全號碼碼無效:日期無效,不能為零或超出當月最大值(%0:s)‘, [sDate]);
      Exit;
    end;
  end;
  if IsLeapYear(StrToInt(sCentury sYear2Bit)) = True then
  begin
    FebDayAmt := 29;
  end else
  begin
    FebDayAmt := 28;
  end;
  if (iMonth in [2]) then
  begin
    if (iDate in [01..FebDayAmt]) = False then
    begin
      Result := Format(‘社會安全號碼碼無效:日期無效,不能為零或超出當月最大值(%0:s)‘, [sDate]);
      Exit;
    end;
  end;
  if CRCFact <> ‘‘ then
  begin
    CRCTh := GetVerifyBit(APID);
    if CRCFact <> CRCTh then
    begin
      Result := Format(‘社會安全號碼碼無效:校正位(第18位)錯:(%0:s)‘, [APID]);
      Exit;
    end;
  end;
  Result := ‘1‘;
end;

Delphi:校正手機號及社會安全號碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.