Original address: http://blog.csdn.net/ytfy12/article/details/11679625
Functional requirements: Since more than 15,000 insured employees need to declare and inspect their ID card numbers, it is necessary to create a
software function that can verify the validity of ID card numbers, which can be processed as follows:
1. Check whether the ID card is empty
2. Check whether the length of the ID card is 15 or 18 digits.
3. Check the validity of the date of birth of the ID card.
4. Check the validity of the format of the ID card
. bits plus check digit.
6. It is written in the form of a function for calling and returning an illegal type.
Implementation environment: PL/SQL 7.0
has compiled three functions: sfzhcheck() takes the character ID number as a parameter, returns "correct" and various illegal error types
isnumber() takes the character ID number as a parameter to verify whether the Legal format, return true or false value
isdate() Take a character date in the format of 2008-08-26 as a parameter, verify whether it is a legal date
and the year is limited to the 20th century.
The specific implementation is as follows:
1. Date judgment function
create or replace isdate(rq in varchar2) return boolean is
Result boolean;
lyear integer;
lmonth integer;
lday integer;
begin
result:=true;
lyear:=to_number(substr(rq,1,4));
lmonth:=to_number(substr(rq,6,2));
lday:=to_number(substr(rq,9,2));
if lyear>2000 or lyear<1900 then
Result:=false;
end if;
if lmonth<1 or lmonth>12 then
Result:=false;
end if ;
if mod(lyear,4)=0 and lyear/100<>Floor(lyear/100) or mod(lyear,400)=0 then
if (lmonth=2 and (lday<1 or lday>29)) then
Result:=false;
end if;
else
if (lmonth=2 and (lday<1 or lday>28)) then
Result:=false;
end if;
end if;
if lmonth in (1,3,5,7,8,10,12) and (lday<1 or lday>31) then
Result:=false;
end if;
if lmonth in (4,6,9,11) and (lday<1 or lday>30) then
Result:=false;
end if;
return(Result);
end isdate;
2、数字格式判断函数
create or replace IsNumber(Name in varchar2) return boolean is
Result boolean;
sz varchar2(10);
pos integer;
zf varchar2(1);
cd integer;
begin
sz:='0123456789';
Result:=true;
pos:=1;
cd:=length(name);
zf:=substr(name,pos,1);
while pos <= cd loop
if instr(sz,zf,1)!=0 then
pos:=pos+1;
zf:=substr(name,pos,1);
else
Result:=false;
return(Result);
end if;
end loop;
return(Result);
end IsNumber;
3、身份证合法校验函数
create or replace sfzhcheck(sfzh in varchar2) return varchar2 is
Result varchar2(30);
err varchar2(30);
sfzh1 varchar2(20);
sfzh2 varchar2(20);
sfzh_len number(2,0);
rql varchar2(10);
nn varchar2(36);
ss varchar2(11);
ll_Sum integer;
i integer;
ls_V varchar2(2);
begin
nn:= '070910050804020106030709100508040201';
ss:= '10X98765432';
err:='correct';
sfzh1:=sfzh;
---Check if the ID card is empty
if sfzh1 is null then
err:='The ID card is empty';
result:=err;
return( Result);
end if ;
--- Check if the ID card length satisfies
sfzh_len:=length(trim(nvl(sfzh1,'')));
if not (sfzh_len=15 or sfzh_len=18) then
err:='ID card Length is wrong';
result:=err;
return(Result);
end if;
----Check if the ID card format is correct
if not (
(sfzh_len = 15 and IsNumber(sfzh1)) or
(sfzh_len = 18 And (IsNumber(sfzh1 ) or (IsNumber(substr(sfzh1,1, 17)) And Upper(substr(sfzh1,18, 1)) = 'X')))) Then
err:= ' ID card format error';
result:=err;
return(Result);
End If ;
---Check if the birth date of the ID card is correct
if sfzh_len = 15 Then
rql:= '19' || substr(sfzh1, 7, 2) || '-' | | substr(sfzh1, 9, 2) || '-' || substr(sfzh, 11, 2);
Else
rql :=substr(sfzh1, 7, 4) || '-' || substr(sfzh1, 11, 2) || '-' || substr(sfzh1, 13, 2);
End If;
software function that can verify the validity of ID card numbers, which can be processed as follows:
1. Check whether the ID card is empty
2. Check whether the length of the ID card is 15 or 18 digits.
3. Check the validity of the date of birth of the ID card.
4. Check the validity of the format of the ID card
. bits plus check digit.
6. It is written in the form of a function for calling and returning an illegal type.
Implementation environment: PL/SQL 7.0
has compiled three functions: sfzhcheck() takes the character ID number as a parameter, returns "correct" and various illegal error types
isnumber() takes the character ID number as a parameter to verify whether the Legal format, return true or false value
isdate() Take a character date in the format of 2008-08-26 as a parameter, verify whether it is a legal date
and the year is limited to the 20th century.
The specific implementation is as follows:
1. Date judgment function
create or replace isdate(rq in varchar2) return boolean is
Result boolean;
lyear integer;
lmonth integer;
lday integer;
begin
result:=true;
lyear:=to_number(substr(rq,1,4));
lmonth:=to_number(substr(rq,6,2));
lday:=to_number(substr(rq,9,2));
if lyear>2000 or lyear<1900 then
Result:=false;
end if;
if lmonth<1 or lmonth>12 then
Result:=false;
end if ;
if mod(lyear,4)=0 and lyear/100<>Floor(lyear/100) or mod(lyear,400)=0 then
if (lmonth=2 and (lday<1 or lday>29)) then
Result:=false;
end if;
else
if (lmonth=2 and (lday<1 or lday>28)) then
Result:=false;
end if;
end if;
if lmonth in (1,3,5,7,8,10,12) and (lday<1 or lday>31) then
Result:=false;
end if;
if lmonth in (4,6,9,11) and (lday<1 or lday>30) then
Result:=false;
end if;
return(Result);
end isdate;
2、数字格式判断函数
create or replace IsNumber(Name in varchar2) return boolean is
Result boolean;
sz varchar2(10);
pos integer;
zf varchar2(1);
cd integer;
begin
sz:='0123456789';
Result:=true;
pos:=1;
cd:=length(name);
zf:=substr(name,pos,1);
while pos <= cd loop
if instr(sz,zf,1)!=0 then
pos:=pos+1;
zf:=substr(name,pos,1);
else
Result:=false;
return(Result);
end if;
end loop;
return(Result);
end IsNumber;
3、身份证合法校验函数
create or replace sfzhcheck(sfzh in varchar2) return varchar2 is
Result varchar2(30);
err varchar2(30);
sfzh1 varchar2(20);
sfzh2 varchar2(20);
sfzh_len number(2,0);
rql varchar2(10);
nn varchar2(36);
ss varchar2(11);
ll_Sum integer;
i integer;
ls_V varchar2(2);
begin
nn:= '070910050804020106030709100508040201';
ss:= '10X98765432';
err:='correct';
sfzh1:=sfzh;
---Check if the ID card is empty
if sfzh1 is null then
err:='The ID card is empty';
result:=err;
return( Result);
end if ;
--- Check if the ID card length satisfies
sfzh_len:=length(trim(nvl(sfzh1,'')));
if not (sfzh_len=15 or sfzh_len=18) then
err:='ID card Length is wrong';
result:=err;
return(Result);
end if;
----Check if the ID card format is correct
if not (
(sfzh_len = 15 and IsNumber(sfzh1)) or
(sfzh_len = 18 And (IsNumber(sfzh1 ) or (IsNumber(substr(sfzh1,1, 17)) And Upper(substr(sfzh1,18, 1)) = 'X')))) Then
err:= ' ID card format error';
result:=err;
return(Result);
End If ;
---Check if the birth date of the ID card is correct
if sfzh_len = 15 Then
rql:= '19' || substr(sfzh1, 7, 2) || '-' | | substr(sfzh1, 9, 2) || '-' || substr(sfzh, 11, 2);
Else
rql :=substr(sfzh1, 7, 4) || '-' || substr(sfzh1, 11, 2) || '-' || substr(sfzh1, 13, 2);
End If;
if not isdate(rql) then
err:= 'The birth date of the ID card is wrong';
result:=err;
return(Result);
end if;
----The gender is not checked and can be expanded
----check the ID card Check if the bit is correct
sfzh2:=sfzh1;
If sfzh_len = 15 Then
sfzh2 := substr(sfzh1,1, 6) ||+ '19' ||substr(sfzh1,7,9);
end if;
i:=1;
ll_sum:=0;
while i<=17 loop
ll_Sum :=ll_Sum+ to_number(substr(sfzh2, i, 1)) * to_number(substr(nn,(i-1)*2+1,2));
i:= i+1;
end loop;
ls_V := substr(ss,(Mod(ll_Sum, 11) + 1),1);
If sfzh_len = 18 Then
If upper(substr(sfzh1,18, 1)) <> upper(ls_V ) Then
err:= 'The check digit of the ID card is incorrect';
result:=err;
return(Result);
End If;
Else
sfzh2:=sfzh2||ls_V;
End If;
result:=err;
return(Result);
end sfzhcheck;
err:= 'The birth date of the ID card is wrong';
result:=err;
return(Result);
end if;
----The gender is not checked and can be expanded
----check the ID card Check if the bit is correct
sfzh2:=sfzh1;
If sfzh_len = 15 Then
sfzh2 := substr(sfzh1,1, 6) ||+ '19' ||substr(sfzh1,7,9);
end if;
i:=1;
ll_sum:=0;
while i<=17 loop
ll_Sum :=ll_Sum+ to_number(substr(sfzh2, i, 1)) * to_number(substr(nn,(i-1)*2+1,2));
i:= i+1;
end loop;
ls_V := substr(ss,(Mod(ll_Sum, 11) + 1),1);
If sfzh_len = 18 Then
If upper(substr(sfzh1,18, 1)) <> upper(ls_V ) Then
err:= 'The check digit of the ID card is incorrect';
result:=err;
return(Result);
End If;
Else
sfzh2:=sfzh2||ls_V;
End If;
result:=err;
return(Result);
end sfzhcheck;