ITPub博客

首页 > 应用开发 > IT综合 > 验证身份证函数

验证身份证函数

原创 IT综合 作者:gowin 时间:2006-06-01 15:26:18 1 删除 编辑

获得校验位数据
获得身份证出生时间
获得身份证性别
判断身份证出生时间是否有效
验证身份证是否有效

[@more@]

CREATE OR REPLACE Function Get_Sfz_Vbit(g_Sfz Varchar2) Return Varchar2 As
--获得校验位数据
Cid Varchar2(4000);
Nsum Int;
Check_Is_Num Int;
Check_Num Int;
Return_Value Varchar2(10);
Begin
If Lengthb(g_Sfz) >= 15 Then
Check_Is_Num := 0;
If Lengthb(g_Sfz) = 15 And Lengthb(Replace(Translate(g_Sfz, '0123456789', ' '), ' ', '')) Is Null Then
Check_Is_Num := 1;
End If;
If (Lengthb(g_Sfz) = 17 Or Lengthb(g_Sfz) = 18) And
Lengthb(Replace(Translate(Substr(g_Sfz, 1, 17), '0123456789', ' '), ' ', '')) Is Null Then
Check_Is_Num := 1;
End If;
If Instr(g_Sfz, Chr(32)) > 0 Then
Check_Is_Num := 0;
End If;
If Check_Is_Num = 1 Then
If Lengthb(g_Sfz) = 15 Then
Cid := Substr(g_Sfz, 1, 6) || '19' || Substr(g_Sfz, 7);
End If;
If Lengthb(g_Sfz) = 17 Or Lengthb(g_Sfz) = 18 Then
Cid := Substr(g_Sfz, 1, 17);
Nsum := Substr(Cid, 1, 1) * 7;
Nsum := Nsum + Substr(Cid, 2, 1) * 9;
Nsum := Nsum + Substr(Cid, 3, 1) * 10;
Nsum := Nsum + Substr(Cid, 4, 1) * 5;
Nsum := Nsum + Substr(Cid, 5, 1) * 8;
Nsum := Nsum + Substr(Cid, 6, 1) * 4;
Nsum := Nsum + Substr(Cid, 7, 1) * 2;
Nsum := Nsum + Substr(Cid, 8, 1) * 1;
Nsum := Nsum + Substr(Cid, 9, 1) * 6;
Nsum := Nsum + Substr(Cid, 10, 1) * 3;
Nsum := Nsum + Substr(Cid, 11, 1) * 7;
Nsum := Nsum + Substr(Cid, 12, 1) * 9;
Nsum := Nsum + Substr(Cid, 13, 1) * 10;
Nsum := Nsum + Substr(Cid, 14, 1) * 5;
Nsum := Nsum + Substr(Cid, 15, 1) * 8;
Nsum := Nsum + Substr(Cid, 16, 1) * 4;
Nsum := Nsum + Substr(Cid, 17, 1) * 2;

Check_Num := 12 - Mod(Nsum, 11);
Return_Value := Check_Num;
If Check_Num = 10 Then
Return_Value := 'X';
End If;
If Check_Num = 11 Then
Return_Value := '0';
End If;
If Check_Num = 12 Then
Return_Value := '1';
End If;
End If;
Else
Return_Value := '';
End If;
Else
Return_Value := '';
End If;
Return Return_Value;
End;
/

CREATE OR REPLACE Function Get_Sfz_Born(g_Sfz Varchar2) Return Varchar2 As
--获得身份证出生时间
Return_Value Varchar2(10);
Begin
If Lengthb(g_Sfz) = 15 Then
Return_Value := '19' || Substr(g_Sfz, 7, 2) || '-' || Substr(g_Sfz, 9, 2) || '-' || Substr(g_Sfz, 11, 2);
End If;
If Lengthb(g_Sfz) = 18 Then
Return_Value := Substr(g_Sfz, 7, 4) || '-' || Substr(g_Sfz, 11, 2) || '-' || Substr(g_Sfz, 13, 2);
End If;
Return Return_Value;
End;
/

CREATE OR REPLACE Function Get_Sfz_Sex(g_Sfz Varchar2) Return Varchar2 As
--获得身份证性别
l_Sfz Int;
Check_Is_Num Int;
Return_Value Varchar2(10);
Begin
If Lengthb(g_Sfz) = 15 And Lengthb(Replace(Translate(g_Sfz, '0123456789', ' '), ' ', '')) Is Null Then
Check_Is_Num := 1;
End If;
If (Lengthb(g_Sfz) = 17 Or Lengthb(g_Sfz) = 18) And
Lengthb(Replace(Translate(Substr(g_Sfz, 1, 17), '0123456789', ' '), ' ', '')) Is Null Then
Check_Is_Num := 1;
End If;
If Check_Is_Num = 1 Then
If Lengthb(g_Sfz) = 15 Then
l_Sfz := Substr(g_Sfz, 15, 1);
End If;
If Lengthb(g_Sfz) = 18 Then
l_Sfz := Substr(g_Sfz, 17, 1);
End If;
If Mod(l_Sfz, 2) = 1 Then
Return_Value := '男';
Else
Return_Value := '女';
End If;
End If;
Return Return_Value;
End;
/

CREATE OR REPLACE Function If_Sfz_Born_Verify(g_Sfz Varchar2) Return Varchar2 As
--判断身份证出生时间是否有效
y Int;
m Int;
d Int;
Check_Is_Num Varchar2(10);
Return_Value Varchar2(10);
Begin
If Lengthb(g_Sfz) = 15 And Lengthb(Replace(Translate(g_Sfz, '0123456789', ' '), ' ', '')) Is Null Then
Check_Is_Num := 1;
End If;
If (Lengthb(g_Sfz) = 17 Or Lengthb(g_Sfz) = 18) And
Lengthb(Replace(Translate(Substr(g_Sfz, 1, 17), '0123456789', ' '), ' ', '')) Is Null Then
Check_Is_Num := 1;
End If;
Return_Value := 1;
If Check_Is_Num = 1 Then
If Lengthb(g_Sfz) = 15 Then
y := '19' || Substr(g_Sfz, 7, 2);
m := Substr(g_Sfz, 9, 2);
d := Substr(g_Sfz, 11, 2);
End If;
If Lengthb(g_Sfz) = 18 Then
y := Substr(g_Sfz, 7, 4);
m := Substr(g_Sfz, 11, 2);
d := Substr(g_Sfz, 13, 2);
End If;

If y < 1900 And y > 2050 Then
Return_Value := 0;
End If;
If (m = 1 Or m = 3 Or m = 5 Or m = 7 Or m = 8 Or m = 10 Or m = 12) And (d < 1 Or d > 31) Then
Return_Value := 0;
End If;
If m = 2 Then
If (Mod(y, 4) = 0 And Mod(y, 100) <> 0) Or Mod(y, 400) = 0 Then
If d < 1 Or d > 29 Then
Return_Value := 0;
End If;
Else
If d < 1 Or d > 28 Then
Return_Value := 0;
End If;
End If;
End If;
If m = 4 Or m = 6 Or m = 9 Or m = 11 Then
If d < 1 Or d > 30 Then
Return_Value := 0;
End If;
End If;
Else
Return_Value := 0;
End If;
Return Return_Value;
Exception
When Others Then
Return 0;
End;
/

CREATE OR REPLACE Function If_Sfz_Verify(g_Sfz Varchar2) Return Number As
--验证身份证是否有效
Return_Value Number;
Check_Num Varchar2(10);
Begin
Return_Value := 1;
If Lengthb(g_Sfz) = 18 Then
Check_Num := Get_Sfz_Vbit(g_Sfz);
If Check_Num <> Substr(g_Sfz, 18, 1) Then
Return_Value := 0;
End If;
Else
If Lengthb(g_Sfz) <> 15 Then
Return_Value := 0;
End If;
End If;

If Instr(g_Sfz, Chr(32)) > 0 Then
Return_Value := 0;
End If;

If If_Sfz_Born_Verify(g_Sfz) = 0 Then
Return_Value := 0;
End If;
Return Return_Value;
End;
/

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28429/viewspace-839136/,如需转载,请注明出处,否则将追究法律责任。

下一篇: 鴕鳥的智慧
请登录后发表评论 登录
全部评论

注册时间:2008-02-02

  • 博文量
    55
  • 访问量
    954230