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 ...
随机推荐
- 搞定redis面试--Redis的过期策略?手写一个LRU?
1 面试题 Redis的过期策略都有哪些?内存淘汰机制都有哪些?手写一下LRU代码实现? 2 考点分析 1)我往redis里写的数据怎么没了? 我们生产环境的redis怎么经常会丢掉一些数据?写进去了 ...
- 短视频SDK简单易用——来自RDSDK.COM
锐动天地为开发者提供短视频编辑.视频直播.特效.录屏.编解码.视频转换,等多种解决方案,涵盖PC.iOS.Android多平台.以市场为导向,不断打磨并创新技术,在稳定性,兼容性,硬件设备效率优化上千 ...
- addslashes,stripslashes
官方介绍: (PHP 4, PHP 5) addslashes — 使用反斜线引用字符串 返回字符串,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线.这些字符是单引号(’).双引号(”). ...
- Mac eclipse java6环境安装
由于旧版adtbundle eclipse需要java se6版本支持,而较新版本mac系统默认安装较高的java版本,所以这里需要卸载高版本jdk(1.8),然后安装1.6 mac删除jdk jav ...
- codeforces_1075_C. The Tower is Going Home
http://codeforces.com/contest/1075/problem/C 题意:一个长宽均为1e9的棋盘,n个垂直障碍在x列无限长,m个水平障碍在第y行从第x1列到x2列.可以水平和垂 ...
- mycat+ mysql集群 分库分表
mycat介绍Mycat数据库分库分表中间件国内最活跃的.性能最好的开源数据库中间件!Mycat关键特性关键特性支持SQL92标准支持MySQL.Oracle.DB2.SQL Server.Postg ...
- python 内置2to3工具将python2代码转换为python3代码
python2与python3代码不兼容,如果需要python2代码在python3环境下运行,需要将代码进行转换,本文介绍使用python3内置工具2to3.py对代码进行转换 一:2to3.py在 ...
- 获取select标签选中的值的三种方式
var obj = document.getElementByIdx_x(”testSelect”); //定位id var index = obj.selectedIndex; // 选中索引 va ...
- vue全选和取消全选
代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- PHP 下基于 php-amqp 扩展的 RabbitMQ 简单用例 (一) -- 安装 AMQP 扩展和 Direct Exchange 模式
Windows 安装 amqp 扩展 RabbitMQ 是基于 amqp(高级消息队列协议) 协议的.使用 RabbitMQ 前必须为 PHP 安装相应的 amqp 扩展. 下载相应版本的 amqp ...