身份证校验[pb源码]

来源:互联网 发布:dw软件下载 编辑:程序博客网 时间:2024/05/01 01:10
public function integer isvalid (string as_idcard, integer ai_mode, ref string as_refvalue);
/*************身份证检验以及识别地区**********************

原型: integer IsVaid(string as_idcard , integer ai_mode , ref string as_refvalue) ; 
参数说明:as_idcard 传入的身份证号
 as_idcard
  XXXXXX YYYYMMDD WWW P
ls_zone ls_date ls_sn li_vi
ls_year ls_month ls_day

 ai_mode 所使用识别码的模式
  值:MODE_1995 95版的区域代码
     MODE_2002 02版的区域代码
as_refvalue 返回该身份证所携带的信息
当返回值>0时返回空
功能: 校验身份证的有效性,不对其真实性校验
返回值:
0  身份证校验正确
16  区域码错误
8  年 错误
4  月 错误
2  日 错误
1 效验码错误

还可返回任意组合的值
********************************************************/
constant integer MODE_1995 = 0
constant integer MODE_2002 = 1
integer li_ret , eof , li_vi , ret_value = 0
string ls_file , ls_str , ls_code 
string ls_zone , ls_date , ls_sn , ls_year , ls_month , ls_day , ls_vi
integer IDCARDMODE

if len(string(longlong(as_idcard))) = 18 or ( len(string(longlong(left(as_idcard,17)))) = 17 and (right as_idcard , 1) = 'x' or right(as_idcard , 1) = 'X')) then //18位判断
IDCARDMODE = 18
else
if len(string(longlong(as_idcard))) = 15 then //15位判断
IDCARDMODE = 15
else
return 255 //身份证号错误,有非法字符
end if
end if



if ai_mode = MODE_1995 then
ls_file = "GBT1995"
else
ls_file = "GBT2002"
end if

li_ret = FileOpen(ls_file, LineMode!, Read!, LockReadWrite!)

ls_zone = left(as_idcard , 6)

if IDCARDMODE= 18 then


ls_date = mid(as_idcard , 7 , 8) //18位
ls_sn = mid(as_idcard , 15 , 3)
ls_vi = right(as_idcard , 1)

if IsNumber(ls_vi) then
li_vi= integer(ls_vi)
else
if ls_vi='X' or ls_vi='x'  then
li_vi = 10
else
li_vi = -1
end if
end if

ls_year = left(ls_date , 4) //年
//年份错误判断(18)
// if year(today()) - integer(ls_year) < 18 then //18周岁成年判断
if year(today()) < integer(ls_year)  then
ret_value = ret_value + 8
end if
end if

if IDCARDMODE = 15 then
ls_date = mid (as_idcard ,7 , 6) //15位
ls_sn = mid(as_idcard , 13 , 3)
ls_year = left(ls_date , 2) //年
//年份错误判断(15)
// if year(today()) - integer('19' + ls_year) < 18 then //18周岁成年判断
if year(today()) < integer('19' + ls_year)  then
ret_value = ret_value + 8
end if
end if


ls_month = mid(ls_date , 5 , 2) //月

//月份错误判断
if ls_month > "12" then
ret_value = ret_value + 4
end if

ls_day = right(ls_date , 2) //日
string ls_maxd
choose case ls_month
case '01','03','05','07','08','10','12'
ls_maxd = '31'
case '04','06','09','11'
ls_maxd = '30'
case '02'
ls_maxd = '29'
end choose
//天数错误判断
if ls_day > ls_maxd then
ret_value = ret_value + 2
end if

boolean zexist = false
string ls_addr

do
eof = FileReadEx(li_ret , ls_str)
ls_code = left(ls_str,6)
if ls_zone = ls_code then
ls_addr = right (ls_str , len(ls_str) - 6)
zexist = true
exit
end if
loop  while  eof > -100 ; 
fileclose(li_ret)

//区域错误判断
if zexist = false then
ret_value = ret_value + 16
end if

if IDCARDMODE = 18 then

//效验位的判断(仅对18位有效)
string ls_longval
int w[17] = {7 ,9, 10 ,5, 8, 4, 2 ,1 ,6, 3, 7, 9, 10, 5, 8, 4, 2} 
int v[11] = {1,0,10,9,8,7,6,5,4,3,2} // 10 = 'X'
int i , isum = 0 , itype
ls_longval = left(as_idcard , 17)
for i = 1 to 17
isum = isum + w[i]*integer(mid(ls_longval , i , 1))
next
itype = mod(isum , 11) + 1
if v[itype] <> li_vi then
ret_value = ret_value + 1
end if

end if

if ret_value = 0 then
string ls_birthday , ls_sex
ls_sex = string(1 - mod(integer(ls_sn) , 2)) //0表示男,1表示女
ls_birthday = ls_year + ls_month + ls_day
as_refvalue = trim(ls_addr + '|' + ls_birthday + '|' + ls_sex)
else
as_refvalue = '
end if

return ret_value
end function
 
原创粉丝点击