//校验手机号  
function
 IsMobileNumber( num:string ):boolean;
  begin
    Result:=False;
    if length( trim( Num ) ) <> 11 then Exit;
    if ( ( copy( num, 12) <> '13' and ( copy( num , 12) <> '15' ) ) then Exit;
    try
      StrToInt( copy( num, 39 ) );
      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:校验手机号及身份证号的更多相关文章

  1. js 随机生成姓名、手机号、身份证号、银行卡号

    开发测试的时候,经常需要填写姓名.手机号.身份证号.银行卡号,既要符合格式要求.又不能重复.大家会到网上搜各种生成器.能不能自己写一个简单的生成器呢.下面是随机生成姓名.手机号.身份证号.银行卡号的j ...

  2. Laravel 中自定义 手机号和身份证号验证

    首先在 Providers\AppServiceProvider.php 文件中自定义 手机号和身份证号验证 // AppServiceProvider.php 文件 <?php namespa ...

  3. jquery正则表达式验证(手机号、身份证号、中文名称)

    这篇文章主要介绍了jquery正则表达式验证,实现手机号.身份证号.中文名称验证,感兴趣的小伙伴们可以参考一下 本文实例需要验证的内容:中文姓名.手机号.身份证和地址,验证方法分享给大家供大家参考,具 ...

  4. [前端] jquery验证手机号、身份证号、中文名称

    验证: 中文姓名.手机号.身份证和地址 HTML(表单): <form action=""> <div class="form-group"& ...

  5. 使用mysql将手机号、身份证号等字段进行脱敏

    -- 脱敏姓名 UPDATE wb_person_message SET `name`=(if(LENGTH(name)>6,CONCAT(LEFT(name,1), '**' ),CONCAT ...

  6. C#中使用正则表达式验证电话号码、手机号、身份证号、数字和邮编

      验证电话号码的主要代码如下: public bool IsTelephone(string str_telephone) { return System.Text.RegularExpressio ...

  7. 1.2.2 Excel中手机号或身份证号批量加密星号

    在对应的单元格中我们输入公式: =LEFT(C4,3)&"****"&RIGHT(C4,4)或=MID(C4,1,3)&"****"&a ...

  8. [转载]C#中使用正则表达式验证电话号码、手机号、身份证号、数字和邮编

    原文出处:https://www.cnblogs.com/wuhuisheng/archive/2011/03/23/1992652.html 验证电话号码的主要代码如下: public bool I ...

  9. C# 中使用正则表达式验证电话号码、手机号、身份证号

    验证电话号码的主要代码如下: public bool IsTelephone(stringstr_telephone) { returnSystem.Text.RegularExpressions.R ...

随机推荐

  1. AJPFX关于多态中的动态绑定和静态绑定的总结

    在多态中:成员变量和静态方法编译和运行都看左边:成员方法编译看左边,运行看右边,这是为什么:在Java中存在两种绑定方式,一种为静态绑定,又称作早期绑定.另一种就是动态绑定,亦称为后期绑定1.静态绑定 ...

  2. 平衡图片负载,提升web站点访问体验

    最近给分公司做官方网站,内网测试一切ok,发布至云端后,体验惊人——公司外网网速渣渣(十几k~几十k),更加要命的是,网站的高清图,根本就加载不出来,几秒,十几秒过去了,仍然在转圈圈,如下图... 于 ...

  3. Angular 组件之间的传值

    第一种方法(传单个或者多个参数): 主页面方法: 先添加引用:private _routes: Router, Details(PBSCode) { this._routes.navigate(['p ...

  4. C语言基础-运算符

    sizeof()运算符 •sizeof可以用来计算一个变量或者一个常量.一种数据类型所占的内存字节数 •sizeof一共有3种形式   1.sizeof( 变量\常量 )      sizeof(10 ...

  5. 浅谈Key-value 存储——SILT

    摘要:本文以文章SILT: A Memory Efficient High Performance Key-Value Store 为基础,探讨SILT存储系统是如何实现内存占用低和高性能的设计目标, ...

  6. 使用迅为iTOP-iMX6开发板-uboot-修改默认环境变量

    iTOP-iMX6 开发板烧写好之后,默认是 android 系统 9.7 寸屏幕的系统参数和屏幕参数.如下图.本文档主要介绍如何修改默认启动参数. 1. 重要的环境变量比较重要的环境变量或者说经常使 ...

  7. Jmeter之断言——检查点

    Jmeter里的断言相当于lr中的检查点.用于检查测试中得到的响应数据等是否符合预期,用以保证性能测试过程中的数据交互与预期一致. 使用断言的目的:在request的返回层面增加一层判断机制:因为re ...

  8. selenium兼容非标准chrome内核的浏览器

    多浏览器兼容性测试(1) RIDE已经支持多浏览器兼容性测试,例如: firefox ie chrome safari 但是,项目要求支持360极速和360安全浏览器.所以,我们需要增加代码让RIDE ...

  9. fabric的安装

    https://blog.csdn.net/lepton126/article/details/79148027

  10. Win10上 visual studio设置为本地IIS运行网站时 必须以管理员身份加载项目的解决方法

    右键,选择“兼容性疑难解答”. 选择“疑难解答程序” 选择“该程序需要附加权限” 点击测试程序 点击下一步 选择 是,为此程序保存这些设置