vbscript和javascript版的15位, 18位的身份证号码的验证函数.以及根据身份证取省份,生日,性别
程序员文章站
2022-03-21 11:02:01
工作中需要用到身份证验证,还要支持检查15位和18位。 我一时手懒,问同事有没有现成的函数可用,同事google了一下,扔给我一个 asp-vbscript版本的...
工作中需要用到身份证验证,还要支持检查15位和18位。
我一时手懒,问同事有没有现成的函数可用,同事google了一下,扔给我一个 asp-vbscript版本的函数。
可我这边是客户端javascript呀,于是用改 vbs-->js
改完发现蛮好用的,同时发现 vbscript真的很啰嗦,居然写了50多行,我改成js 只用了11行。
当然,我并完全是指vbscript啰嗦,那个写这段vbs代码的无名氏也是个唐僧性格.....
先放出 vbscript代码,
<script language="vbscript">
'功能:检查身份证号码
function checkcardid(e)
arrverifycode = split("1,0,x,9,8,7,6,5,4,3,2", ",")
wi = split("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2", ",")
checker = split("1,9,8,7,6,5,4,3,2,1,1", ",")
if len(e) < 15 or len(e) = 16 or len(e) = 17 or len(e) > 18 then
checkcardid= "身份证号共有 15 码或18位"
checkcardid = false
exit function
end if
dim ai
if len(e) = 18 then
ai = mid(e, 1, 17)
elseif len(e) = 15 then
ai = e
ai = left(ai, 6) & "19" & mid(ai, 7, 9)
end if
if not isnumeric(ai) then
checkcardid= "身份证除最后一位外,必须为数字!"
exit function
end if
dim stryear, strmonth, strday
stryear = cint(mid(ai, 7, 4))
strmonth = cint(mid(ai, 11, 2))
strday = cint(mid(ai, 13, 2))
birthday = trim(stryear) + "-" + trim(strmonth) + "-" + trim(strday)
if isdate(birthday) then
if datediff("yyyy",now,birthday)<-140 or cdate(birthday)>date() then
checkcardid= "身份证输入错误!"
exit function
end if
if strmonth > 12 or strday > 31 then
checkcardid= "身份证输入错误!"
exit function
end if
else
checkcardid= "身份证输入错误!"
exit function
end if
dim i, totalmulaiwi
for i = 0 to 16
totalmulaiwi = totalmulaiwi + cint(mid(ai, i + 1, 1)) * wi(i)
next
dim modvalue
modvalue = totalmulaiwi mod 11
dim strverifycode
strverifycode = arrverifycode(modvalue)
ai = ai & strverifycode
checkcardid = ai
if len(e) = 18 and e <> ai then
checkcardid= "身份证输入错误!"
exit function
end if
end function
</script>
再放出 javascript版本代码
function checkid(pid){
//检查身份证号码 go_rush(阿舜) from http://ashun.cnblogs.com
var arrverifycode = [1,0,"x",9,8,7,6,5,4,3,2];
var wi = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];
var checker = [1,9,8,7,6,5,4,3,2,1,1];
if(pid.length != 15 && pid.length != 18) return "身份证号共有 15 码或18位";
var ai=pid.length==18 ? pid.substring(0,17) : pid.slice(0,6)+"19"+pid.slice(6,16);
if (!/^\d+$/.test(ai)) return "身份证除最后一位外,必须为数字!";
var yyyy=ai.slice(6,10) , mm=ai.slice(10,12)-1 , dd=ai.slice(12,14);
var d=new date(yyyy,mm,dd) , now=new date();
var year=d.getfullyear() , mon=d.getmonth() , day=d.getdate();
if (year!=yyyy || mon!=mm || day!=dd || d>now || year<1940) return "身份证输入错误!";
for(var i=0,ret=0;i<17;i++) ret+=ai.charat(i)*wi[i];
ai+=arrverifycode[ret %=11];
return pid.length ==18 && pid != ai?"身份证输入错误!":ai;
};
至于身份证算法的原理,请参阅: http://www.google.com/
最后,根据身份证号码取 省份,生日,性别就很简单了
<script language="javascript">
var id="342201570202003"
//根据身份证取 省份,生日,性别 go_rush(阿舜) from http://ashun.cnblogs.com/
function getinfo(id){
var arr=[null,null,null,null,null,null,null,null,null,null,null,"北京","天津","河北","山西","内蒙古"
,null,null,null,null,null,"辽宁","吉林","黑龙江",null,null,null,null,null,null,null,"上海"
,"江苏","浙江","安微","福建","江西","山东",null,null,null,"河南","湖北","湖南","广东","广西","海南"
,null,null,null,"重庆","四川","贵州","云南","*",null,null,null,null,null,null,"陕西","甘肃"
,"青海","宁夏","*",null,null,null,null,null,"*",null,null,null,null,null,null,null,null
,null,"香港","澳门",null,null,null,null,null,null,null,null,"国外"]
id=checkid(id)
if (isnan(id)) return "错误的身份证号码"
var id=string(id), prov=arr[id.slice(0,2)] , sex=id.slice(14,17)%2? "男" : "女"
var birthday=(new date(id.slice(6,10) , id.slice(10,12)-1 , id.slice(12,14))).tolocaledatestring()
return [prov,birthday,sex]
}
alert(getinfo(id))
</script>
上面的代码只能取到省份,要取到具体城市,请看我的另外一个随笔,
代码只拿我和几个同事的身份证号码测了一下,如果有bug,请指出
我一时手懒,问同事有没有现成的函数可用,同事google了一下,扔给我一个 asp-vbscript版本的函数。
可我这边是客户端javascript呀,于是用改 vbs-->js
改完发现蛮好用的,同时发现 vbscript真的很啰嗦,居然写了50多行,我改成js 只用了11行。
当然,我并完全是指vbscript啰嗦,那个写这段vbs代码的无名氏也是个唐僧性格.....
先放出 vbscript代码,
复制代码 代码如下:
<script language="vbscript">
'功能:检查身份证号码
function checkcardid(e)
arrverifycode = split("1,0,x,9,8,7,6,5,4,3,2", ",")
wi = split("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2", ",")
checker = split("1,9,8,7,6,5,4,3,2,1,1", ",")
if len(e) < 15 or len(e) = 16 or len(e) = 17 or len(e) > 18 then
checkcardid= "身份证号共有 15 码或18位"
checkcardid = false
exit function
end if
dim ai
if len(e) = 18 then
ai = mid(e, 1, 17)
elseif len(e) = 15 then
ai = e
ai = left(ai, 6) & "19" & mid(ai, 7, 9)
end if
if not isnumeric(ai) then
checkcardid= "身份证除最后一位外,必须为数字!"
exit function
end if
dim stryear, strmonth, strday
stryear = cint(mid(ai, 7, 4))
strmonth = cint(mid(ai, 11, 2))
strday = cint(mid(ai, 13, 2))
birthday = trim(stryear) + "-" + trim(strmonth) + "-" + trim(strday)
if isdate(birthday) then
if datediff("yyyy",now,birthday)<-140 or cdate(birthday)>date() then
checkcardid= "身份证输入错误!"
exit function
end if
if strmonth > 12 or strday > 31 then
checkcardid= "身份证输入错误!"
exit function
end if
else
checkcardid= "身份证输入错误!"
exit function
end if
dim i, totalmulaiwi
for i = 0 to 16
totalmulaiwi = totalmulaiwi + cint(mid(ai, i + 1, 1)) * wi(i)
next
dim modvalue
modvalue = totalmulaiwi mod 11
dim strverifycode
strverifycode = arrverifycode(modvalue)
ai = ai & strverifycode
checkcardid = ai
if len(e) = 18 and e <> ai then
checkcardid= "身份证输入错误!"
exit function
end if
end function
</script>
再放出 javascript版本代码
复制代码 代码如下:
function checkid(pid){
//检查身份证号码 go_rush(阿舜) from http://ashun.cnblogs.com
var arrverifycode = [1,0,"x",9,8,7,6,5,4,3,2];
var wi = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];
var checker = [1,9,8,7,6,5,4,3,2,1,1];
if(pid.length != 15 && pid.length != 18) return "身份证号共有 15 码或18位";
var ai=pid.length==18 ? pid.substring(0,17) : pid.slice(0,6)+"19"+pid.slice(6,16);
if (!/^\d+$/.test(ai)) return "身份证除最后一位外,必须为数字!";
var yyyy=ai.slice(6,10) , mm=ai.slice(10,12)-1 , dd=ai.slice(12,14);
var d=new date(yyyy,mm,dd) , now=new date();
var year=d.getfullyear() , mon=d.getmonth() , day=d.getdate();
if (year!=yyyy || mon!=mm || day!=dd || d>now || year<1940) return "身份证输入错误!";
for(var i=0,ret=0;i<17;i++) ret+=ai.charat(i)*wi[i];
ai+=arrverifycode[ret %=11];
return pid.length ==18 && pid != ai?"身份证输入错误!":ai;
};
至于身份证算法的原理,请参阅: http://www.google.com/
最后,根据身份证号码取 省份,生日,性别就很简单了
复制代码 代码如下:
<script language="javascript">
var id="342201570202003"
//根据身份证取 省份,生日,性别 go_rush(阿舜) from http://ashun.cnblogs.com/
function getinfo(id){
var arr=[null,null,null,null,null,null,null,null,null,null,null,"北京","天津","河北","山西","内蒙古"
,null,null,null,null,null,"辽宁","吉林","黑龙江",null,null,null,null,null,null,null,"上海"
,"江苏","浙江","安微","福建","江西","山东",null,null,null,"河南","湖北","湖南","广东","广西","海南"
,null,null,null,"重庆","四川","贵州","云南","*",null,null,null,null,null,null,"陕西","甘肃"
,"青海","宁夏","*",null,null,null,null,null,"*",null,null,null,null,null,null,null,null
,null,"香港","澳门",null,null,null,null,null,null,null,null,"国外"]
id=checkid(id)
if (isnan(id)) return "错误的身份证号码"
var id=string(id), prov=arr[id.slice(0,2)] , sex=id.slice(14,17)%2? "男" : "女"
var birthday=(new date(id.slice(6,10) , id.slice(10,12)-1 , id.slice(12,14))).tolocaledatestring()
return [prov,birthday,sex]
}
alert(getinfo(id))
</script>
上面的代码只能取到省份,要取到具体城市,请看我的另外一个随笔,
代码只拿我和几个同事的身份证号码测了一下,如果有bug,请指出
上一篇: vbs病毒制作之一复制自身的vbs脚本
下一篇: 用VBS检测Guest状态的脚本