Delphi:校验手机号及身份证号
2016-01-16 14:23
441 查看
//校验手机号
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中BCD和Currency类型
- Delphi XE4 inline 的用法
- Delphi:Indy9的IdFTP完全使用
- DELPHI IDFTP
- Delphi 快捷键
- Delphi:ClientDataset+TDataSetProvider的数据保存问题
- Delphi简单的数据操作类
- Delphi DBGrid实现多选
- Delphi ADO的Lookup类型字段的问题
- Delphi与Windows 7下的用户账户控制(UAC)机制
- 常用的delphi 第三方控件
- Delphi判断字符串中是否包含汉字,并返回汉字位置
- delphi TStringList 用法详解
- delphi 截取某个字符之前的字段,,如 1234567-9,要分别得到 - 之前的字符串和 - 之后的字符串
- FreePascal的VMT与Delphi不一致,没有负方向
- 技术笔记:Delphi多线程应用读写锁
- Delphi:ADOConnection连接SQLServer自动断网问题解决
- delphi调用webservice代理设置
- Delphi Show Form
- delphi 升级了控件,但build的时候,总是说版本不对,但是文件版本都是对的