📄 sfz.pas
字号:
unit sfz;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, math, adodb;
type
identitycard = record
sex:integer;
birthday:String;
legal:boolean;
end;
type
Tsfzh = class(TForm)
private
{ Private declarations }
public
function isdate(s:string):boolean;
function birthdaysex(identitynum:string):identitycard;
function Exportaddress(identitynum,datasource:string):string;
{ Public declarations }
end;
var
sfzh: Tsfzh;
implementation
{$R *.dfm}
function Tsfzh.isdate(s:string):boolean;
begin
result:=false;
try
strtodate(trim(s));
result:=true;
except
on econverterror do
result:=false;
end;
end;
const
cCityCode: array[0..65] of string = (
'', '', '', '', '', '', '', '', '', '', '',
'北京', '天津', '河北', '山西', '内蒙古', '', '', '', '', '',
'辽宁', '吉林', '黑龙江', '', '', '', '', '', '', '',
'上海', '江苏', '浙江', '安微', '福建', '江西', '山东', '', '', '',
'河南', '湖北', '湖南', '广东', '广西', '海南', '', '', '',
'重庆', '四川', '贵州', '云南', '西藏', '', '', '', '', '', '',
'陕西', '甘肃', '青海', '宁夏', '新疆');
function Tsfzh.birthdaysex(identitynum:string):identitycard;
var
i:integer;
T:double;
s:string;
begin
result.legal:=true;
s:=trim(identitynum);
I := StrToIntDef(Copy(s, 1, 2), 0);
if ((I > High(cCityCode)) or (cCityCode[I] = ''))and(length(s)<>0) then
begin
result.legal:=false;
exit;
end;
if length(s)=15 then
begin
result.birthday:='19'+copy(s,7,2)+'-'+copy(s,9,2)+'-'+copy(s,11,2);
if not isdate(result.birthday) then
begin
result.legal:=false;
exit;
end;
for i:=1 to 15 do
if not( s[i] in ['0'..'9']) then
begin
result.legal:=false;
exit;
end;
if strtoint(copy(s,15,1))mod 2=1 then
result.sex:=0
else
result.sex:=1;
end
else if length(s)=18 then
begin
result.birthday:=copy(s,7,4)+'-'+copy(s,11,2)+'-'+copy(s,13,2);
if isdate(result.birthday) then
begin
if strtodate(result.birthday)>=Date then
begin
result.legal:=false;
exit;
end;
end
else
begin
result.legal:=false;
exit;
end;
if not( s[18] in ['0'..'9','X','x']) then
begin
result.legal:=false;
exit;
end;
for i:=1 to 17 do
if not( s[i] in ['0'..'9']) then
begin
result.legal:=false;
exit;
end;
if s[18] in ['x', 'X'] then s[18] := 'a';
T := 0;
for I := 18 downto 1 do
T := T + Trunc(Power(2, I - 1)) mod 11 * StrToInt('$'+s[19 - I]);
if Trunc(T) mod 11 <> 1 then
result.legal:=false;
if strtoint(copy(s,17,1))mod 2=1 then
result.sex:=0
else
result.sex:=1;
end
else if length(s)=0 then
begin
result.legal:=true;
result.birthday:=formatdatetime('yyyy-MM-dd',now);
end
else
result.legal:=false;
end;
function tsfzh.Exportaddress(identitynum,datasource:string):string;
var
ado1,ado2,ado3:Tadoquery;
adoc:Tadoconnection;
begin
if (trim(identitynum)<>'')and(birthdaysex(identitynum).legal) then
begin
adoc:=Tadoconnection.Create(self);
adoc.ConnectionString:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;'+
'Initial Catalog=dz;Data Source='+datasource+';Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Use Encryption for Data=False;Tag with column collation when possible=False';
//adoc.ConnectionString:='Provider=MSDASQL.1;Persist Security Info=True;User ID=sa;Data Source='+datasource+';Extended Properties="DSN='+datasource+';UID=sa;APP=Enterprise;DATABASE=dz";Initial Catalog=dz';
adoc.Connected;
adoc.LoginPrompt:=false;
ado1:=Tadoquery.Create(self);
ado2:=Tadoquery.Create(self);
ado3:=Tadoquery.Create(self);
ado1.Connection:=adoc;
ado2.Connection:=adoc;
ado3.Connection:=adoc;
ado1.SQL.Text:='select*from dz where a like '''+copy(trim(identitynum),1,2)+'%''';
ado1.Open;
ado2.SQL.Text:='select*from dz where a like '''+copy(trim(identitynum),1,4)+'%''';
ado2.Open;
ado3.SQL.Text:='select*from dz where a like '''+copy(trim(identitynum),1,6)+'%''';
ado3.Open;
if(ado1.RecordCount>0)and(ado2.RecordCount>0)and(ado3.RecordCount=0)then
if(trim(ado1['b'])=trim(ado2['b']))or(pos('县',ado2['b'])>0) then
result:=ado1['b']
else
result:=ado1['b']+ado2['b'];
if(ado1.RecordCount>0)and(ado2.RecordCount=0)and(ado3.RecordCount=0)then
result:=ado1['b'];
if(ado1.RecordCount>0)and(ado2.RecordCount>0)and(ado3.RecordCount>0)then
begin
if(trim(ado1['b'])=trim(ado2['b'])) then
result:=ado1['b']
else
begin
result:=ado1['b']+ado2['b']+ado3['b'];
if(pos('县',ado3['b'])>0)or(pos('市',ado3['b'])>0)then
begin
if pos('市辖区',ado3['b'])>0 then
result:=ado1['b']+ado2['b']
else
result:=ado1['b']+ado3['b'];
end;
end;
if(pos('市辖区',ado2['b'])>0)and(pos('市辖区',ado3['b'])=0) then
result:=ado1['b']+ado3['b'];
if(pos('市辖区',ado2['b'])>0)and(pos('市辖区',ado3['b'])>0)then
result:=ado1['b'];
if(pos('县',ado2['b'])>0)then
if(trim(ado2['b'])=trim(ado3['b'])) then
result:=ado1['b']
else
result:=ado1['b']+ado3['b']
end;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -