//校验手机号  
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. Webform 内置对象 Session对象、Application全局对象,ViewState

    Session 每台电脑访问服务器,都有独立的session,key值都一样,内容不一样. 1.session保存在服务器上. 2.session没有持久性,保存周期就是20分钟. 重点: sessi ...

  2. 机器学习-Logistic function(Sigmoid function)

    下面给出H函数  由这个函数生成的曲线称为Sigmoid曲线 先不从数学上说为什么这个模型中二元分类上比线性模型好,单纯从图形上看就可以得到直观的结论  首先Y值域在[0,1],其次图形中中间陡峭而两 ...

  3. Context namespace element 'annotation-config' and its parser class [org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser]

    严重: Exception sending context initialized event to listener instance of class org.springframework.we ...

  4. glm 矩阵乘法得反过来写

  5. 迅为工业级arm开发板i.MX6DL开发板软件硬件全开源

    i.MX6是基于ARM Cortex™-A9架构的高扩展性多核系列应用处理器,促进了如高稳定性工业平板电脑.差异化智能本.前装车载中控系统和超高清电子书阅读器等新一代应用的发展.强劲的3D图形加速引擎 ...

  6. Divide and Conquer_1.最大连续子数组

    给定一个数组,求它的一个子数组,使其求和最大. 这个问题的应用:给定一只股票很多天的价格,计算从哪天买进哪天卖出能获得最大利润. 给定 prices:100   113   98   87   65  ...

  7. Android(java)学习笔记196:ContentProvider使用之内容观察者01

    1. 内容观察者 不属于四大组件,只是内容提供者ContentProvider对应的小功能. 如果发现数据库内容变化了,就会立刻观察到. 下面是逻辑图:       当A应用中银行内部的数据发生变化的 ...

  8. 数据结构算法 - ConcurrentHashMap 源码解析

    五个线程同时往 HashMap 中 put 数据会发生什么? ConcurrentHashMap 是怎么保证线程安全的? 在分析 HashMap 源码时还遗留这两个问题,这次我们站在 Java 多线程 ...

  9. webpack的详细介绍和使用

    // 一个常见的`webpack`配置文件 const webpack = require('webpack'); const HtmlWebpackPlugin = require('html-we ...

  10. 如何修改MFC的图标

    原文:如何修改MFC的图标 修改左上角的图标和任务栏里图标 在对话框构造函数中 CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/) : CDialog(CTestD ...