身份证校验码算法(一)

身份证校验码产生方法:
∑(ai×Wi)(mod 11)

i: 表示号码字符从由至左包括校验码在内的位置序号;

ai 表示第i位置上的号码字符值;

Wi 第i位置上的加权因子,其数值Wi=mod(power(2,(n-1)),11)

i 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Wi 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 1

相应的校验码:
∑(ai×WI)(mod 11) 0 1 2 3 4 5 6 7 8 9 10
校验码字符值ai 1 0 X 9 8 7 6 5 4 3 2

下面是校验函数:

FUNCTION sfzjy(num)
if len(num)=15 then
cID = left(num,6)&"19"&right(num,9)
elseif len(num)=17 or len(num)=18 then
cID = left(num,17)
end if
nSum=mid(cID,1,1) * 7
nSum=nsum+mid(cID,2,1) * 9
nSum=nsum+mid(cID,3,1) * 10
nSum=nsum+mid(cID,4,1) * 5
nSum=nsum+mid(cID,5,1) * 8
nSum=nsum+mid(cID,6,1) * 4
nSum=nsum+mid(cID,7,1) * 2
nSum=nsum+mid(cID,8,1) * 1
nSum=nsum+mid(cID,9,1) * 6
nSum=nsum+mid(cID,10,1) * 3
nSum=nsum+mid(cID,11,1) * 7
nSum=nsum+mid(cID,12,1) * 9
nSum=nsum+mid(cID,13,1) * 10
nSum=nsum+mid(cID,14,1) * 5
nSum=nsum+mid(cID,15,1) * 8
nSum=nsum+mid(cID,16,1) * 4
nSum=nsum+mid(cID,17,1) * 2
'*计算校验位
check_number=12-nsum mod 11
If check_number=10 then
check_number="X"
End if
If check_number=12 then
check_number="1"
End if
If check_number=11 then
check_number="0"
End if
sfzjy=check_number
End function

 

其它校验:
性别与出生年月:

sfznum=身份证号码
lenx=len(sfznum)
if lenx=15 then
yy="19"&mid(xian,7,2)
mm=mid(xian,9,2)
dd=mid(xian,11,2)
aa=mid(xian,15,1) '15位身分证取第十五位,能被2整除为女性

end if
if lenx=18 then
yy=mid(xian,7,4)
mm=mid(xian,11,2)
dd=mid(xian,13,2)
aa=mid(xian,17,1) '18位身分证取第十七位,能被2整除为女性
end if
if aa mod 2=0 then
xb="女"
else
xb="男"
end if

if lenx=18 then
if mid(xian,18,1)<>cstr(sfzjy(xian)) then '如果第十八位校验码不等于计算出的校验码则身份证号码有误.
response.write "提示:身份证校验位错误!"
else
response.write "结果:身份证号码校验为合法号码!"
end if
else '如果输入的是十五位号,则计算出十八位新号
response.write "新身份证:"&left(xian,6)&"19"&right(xian,9)&cstr(sfzjy(xian))
end if

关于户籍判断则而要数据库.这里就不提供了.

摘自: http://www.webdn.com/web_file/program/asp/N0610774/
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
身份证校验码是用来验证身份证号码是否有效的一位数字。校验码是通过对身份证号码的前17位数字按照一定的算法进行处理得到的。如果身份证号码最后一位与计算出的校验码不一致,那么该身份证就是无效的。 下面是使用Python实现身份证校验码的示例代码: ```python def validate_id_card(id_card): # 将身份证号码的前17位转换为整数列表 id_list = [int(x) for x in id_card[:17]] # 加权因子 weight_factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] # 校验码对应值 check_code_dict = { 0: '1', 1: '0', 2: 'X', 3: '9', 4: '8', 5: '7', 6: '6', 7: '5', 8: '4', 9: '3', 10: '2' } # 计算身份证号码前17位与加权因子的乘积之和 sum = 0 for i in range(17): sum += id_list[i] * weight_factor[i] # 取模得到校验码对应的值 check_code = check_code_dict[sum % 11] # 判断身份证号码的校验码是否正确 if check_code == id_card[17]: return True else: return False # 调用函数进行身份证校验 id_card = '110101199001011234' # 身份证号码 result = validate_id_card(id_card) print(result) # 输出:True ``` 这段代码中,我们定义了一个`validate_id_card`函数,该函数接收一个身份证号码作为参数,并返回一个布尔值,表示该身份证号码是否有效。函数内部首先将身份证号码的前17位转换为整数列表,然后根据加权因子和校验码对应值的规则计算出校验码,最后判断身份证号码的校验码是否与计算出的校验码一致。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值