Delphi:校验手机号及身份证号
//校验手机号
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:校验手机号及身份证号的更多相关文章
- js 随机生成姓名、手机号、身份证号、银行卡号
开发测试的时候,经常需要填写姓名.手机号.身份证号.银行卡号,既要符合格式要求.又不能重复.大家会到网上搜各种生成器.能不能自己写一个简单的生成器呢.下面是随机生成姓名.手机号.身份证号.银行卡号的j ...
- Laravel 中自定义 手机号和身份证号验证
首先在 Providers\AppServiceProvider.php 文件中自定义 手机号和身份证号验证 // AppServiceProvider.php 文件 <?php namespa ...
- jquery正则表达式验证(手机号、身份证号、中文名称)
这篇文章主要介绍了jquery正则表达式验证,实现手机号.身份证号.中文名称验证,感兴趣的小伙伴们可以参考一下 本文实例需要验证的内容:中文姓名.手机号.身份证和地址,验证方法分享给大家供大家参考,具 ...
- [前端] jquery验证手机号、身份证号、中文名称
验证: 中文姓名.手机号.身份证和地址 HTML(表单): <form action=""> <div class="form-group"& ...
- 使用mysql将手机号、身份证号等字段进行脱敏
-- 脱敏姓名 UPDATE wb_person_message SET `name`=(if(LENGTH(name)>6,CONCAT(LEFT(name,1), '**' ),CONCAT ...
- C#中使用正则表达式验证电话号码、手机号、身份证号、数字和邮编
验证电话号码的主要代码如下: public bool IsTelephone(string str_telephone) { return System.Text.RegularExpressio ...
- 1.2.2 Excel中手机号或身份证号批量加密星号
在对应的单元格中我们输入公式: =LEFT(C4,3)&"****"&RIGHT(C4,4)或=MID(C4,1,3)&"****"&a ...
- [转载]C#中使用正则表达式验证电话号码、手机号、身份证号、数字和邮编
原文出处:https://www.cnblogs.com/wuhuisheng/archive/2011/03/23/1992652.html 验证电话号码的主要代码如下: public bool I ...
- C# 中使用正则表达式验证电话号码、手机号、身份证号
验证电话号码的主要代码如下: public bool IsTelephone(stringstr_telephone) { returnSystem.Text.RegularExpressions.R ...
随机推荐
- poj2393 Yogurt factory
思路: 贪心. 实现: #include <iostream> #include <cstdio> #include <algorithm> using names ...
- css3中的变换、动画和过渡
变换:分为2d变换和3d变换,但一次只能用一个变换属性,多个的话最后一个会覆盖前面所有最终被浏览器实现,变换可以成为过渡和动画的一个待变参数(类似width,opacity等). 过渡:是动画的初始模 ...
- nvm安装nodejs
1. 安装nvm 下载 nvm-windows解压缩 nvm-windows解压缩 nvm-setup双击运行 nvm-setup.exe选择next选择 [D:\dev][path1] 或 默认路径 ...
- NavigationView的使用
代码已经分享至github:https://github.com/YanYoJun/NavigationDemo 转载请注明原文链接:http://www.cnblogs.com/yanyojun/p ...
- Android开发-下载网络图片并显示到本地
Android下载网络图片的流程是: 发送网络请求->将图片以流的形式下载下来->将流转换为Bitmap并赋给ImageView控件. 注意点 最新的Android系统不可以在主线程上请求 ...
- Android一句代码给Activity定制标题栏
在此之前,使用过几种方法设置标题栏: 1.常规法:这个方法是最常用的了,哪个activity需要什么样的标题栏,就在对应的xml布局设计.缺点:此方法维护起来困难,没有将标题栏的共性抽取出来, 如果要 ...
- InChatter系统开源聊天模块前奏曲
最近在研究WCF,又因为工作中的项目需要,要为现有的系统增加一个聊天模块以及系统消息提醒等,因此就使用WCF做服务器端开发了一个简单的系统. 开发最初学习了东邪孤独大哥的<传说的WCF系列> ...
- C#运用存储过程新增一条记录并返回自动生成的ID
@Hcy黄灿奕:http://blog.sina.com.cn/iihcy 前言: 1.存储过的好处: 存储过程相对于其他的数据库访问方法有以下的优点: (1)重复使用.存储过程可以重复使用,从而可以 ...
- HP11.31安装11.2.0.3实施手册
1 前言 此文档详细描述了Oracle 11gR2 数据库在HP11.31上的安装RAC的检查及安装步骤.文档中#表示root用户执行,$表示grid或oracle用户执行. 2 系统环境 操作系统环 ...
- close - 关闭一个文件描述符
SYNOPSIS 总览 #include <unistd.h> int close(int fd); DESCRIPTION 描述 close 关闭 一个 文件 描述符 , 使它 不在 指 ...