• 全部
  • VBS
  • .NET技术
  • VBA
  • VB网络编程
  • VB多媒体
  • VB数据库
  • VB控件
  • VB COM/DCOM
  • VB基础类
  • VB API
  • 问答

今天查找身份证号检查算法,结果都是C#的。真是郁闷!现在我发布一个可以用于vb.net的身份证号检查算法。顺便散一点分。

marke 2005-03-29 04:41:50
Private Function CheckCidInfo(ByVal cid As String) As String
Dim aCity() As String = {"", "", "", "", "", "", "", "", "", "", _
"", "北京", "天津", "河北", "山西", "内蒙古", "", _
"", "", "", "", "辽宁", "吉林", "黑龙江", "", "", _
"", "", "", "", "", "上海", "江苏", "浙江", "安微", _
"福建", "江西", "山东", "", "", "", "河南", "湖北", _
"湖南", "广东", "广西", "海南", "", "", "", "重庆", _
"四川", "贵州", "云南", "西藏", "", "", "", "", "", _
"", "陕西", "甘肃", "青海", "宁夏", "新疆", "", "", _
"", "", "", "台湾", "", "", "", "", "", "", "", "", _
"", "香港", "澳门", "", "", "", "", "", "", "", "", "国外"}
Dim iSum As Double = 0
Dim info As String = ""
Dim rg As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("^\d{17}(\d|x)$")
Dim mc As System.Text.RegularExpressions.Match = rg.Match(cid)
If mc.Success = False Then
Return ""
End If
cid = cid.ToLower
cid = cid.Replace("x", "a")
If aCity(Integer.Parse(cid.Substring(0, 2))) = "" Then
Return "非法地址"
End If
Try
DateTime.Parse(cid.Substring(6, 4) & "-" & cid.Substring(10, 2) & "-" & cid.Substring(12, 2))
Catch ex As Exception
Return "非法生日"
End Try
Dim i As Integer
For i = 17 To 0 Step -1
iSum = iSum + (System.Math.Pow(2, i) Mod 11) * Integer.Parse(cid.Substring(17 - i, 1), System.Globalization.NumberStyles.HexNumber)
'
Next
If iSum Mod 11 <> 1 Then
Return "非法证号"
End If
Dim strSex As String
If (Integer.Parse(cid.Substring(16, 1)) Mod 2) = 1 Then
strSex = "男"
Else
strSex = "女"
End If
Return aCity(Integer.Parse(cid.Substring(0, 2))) & "," & cid.Substring(6, 4) & "-" & cid.Substring(10, 2) & "-" & cid.Substring(12, 2) & "," & strSex
End Function
...全文
1351 点赞 收藏 47
写回复
47 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
marke 2005-04-13
高手帮我看看好吗?谢谢了!
回复
泡沫游走 2005-04-12
mark
回复
marke 2005-04-12
大家好,我是楼主,我有一个MDI问题,大家帮我看看好吗?谢谢了!网址如下:

http://community.csdn.net/Expert/topic/3767/3767782.xml?temp=.1056482
回复
贝冷 2005-04-11
学习!
回复
laoshaobing 2005-04-11
不错,谢谢!!!
回复
wsccl 2005-04-11
实在太帅了,以后有这种东西,多弄点来给兄弟们分享啊!
回复
wch_1982 2005-04-10
谢谢啦!
回复
chacieyu 2005-04-10
好热闹
回复
张志Access 2005-04-09
谢谢
回复
marke 2005-04-09
谢谢上面给出注释的朋友,身份证验证:iSum = iSum + (System.Math.Pow(2, i) Mod 11) * Integer.Parse(cid.Substring(17 - i, 1), System.Globalization.NumberStyles.HexNumber)
这个式子本身没有什么难度,也可以试着把自己的身份证改两位,它仍然是合法的。为什么要用这样的算法就要问当时身份证的设计者了。
回复
marke 2005-04-09
对于地址的验证,我把上面的函数改进了一下,任何与程序中用户输入的出生年月、地址比较。
有的朋友说代码有问,我用的虽然和上面的有些不同,但是一直没有出现问题,如果那位朋友在用的时候出现问题,麻烦您告诉我好吗?
回复
marke 2005-04-09
好长时间没有来了,没想到有这么多热心的朋友,如果是15位的用下面的函数就可以了。
Public Function getCheckCode(ByVal SFZH As String) As String
Dim strJiaoYan() As Char = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"}
Dim intQuan() As Integer = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1}
Dim strTemp As String
Dim intTemp As String
Dim i As Integer

strTemp = SFZH.Substring(0, 6) & "19" & SFZH.Substring(6)
For i = 0 To strTemp.Length - 1
intTemp = intTemp + Convert.ToInt32(strTemp.Substring(i, 1)) * intQuan(i)
Next
intTemp = intTemp Mod 11

Return strTemp & strJiaoYan(intTemp)
End Function
回复
crystallac 2005-04-09
~!~ 收藏先,谢谢!
回复
yfmine 2005-04-07
VB的语言确实比类C语言繁琐,没有分号换行,CSDN上一贴,格式变了,然后复制回去就可能出问题。。。CSDN也有点烦,怎么不能贴代码。。。。呵呵,GGYY一下。
回复
yfmine 2005-04-07
先用一个正则表达式对身份证的号码进行格式验证,然后依次用Substring取出一段进行地址,生日,还有身份证格式(身份证不能是随便的数字组合,要根据一定的算法进行组合),性别的验证。
'验证身份证号码
'cid=传入的身份证号码
Private Function CheckCidInfo(ByVal cid As String) As String
Dim aCity() As String = {"", "", "", "", "", "", "", "", "", "", _
"", "北京", "天津", "河北", "山西", "内蒙古", "", _
"", "", "", "", "辽宁", "吉林", "黑龙江", "", "", _
"", "", "", "", "", "上海", "江苏", "浙江", "安微", _
"福建", "江西", "山东", "", "", "", "河南", "湖北", _
"湖南", "广东", "广西", "海南", "", "", "", "重庆", _
"四川", "贵州", "云南", "西藏", "", "", "", "", "", _
"", "陕西", "甘肃", "青海", "宁夏", "新疆", "", "", _
"", "", "", "台湾", "", "", "", "", "", "", "", "", _
"", "香港", "澳门", "", "", "", "", "", "", "", "", "国外"}
Dim strCity As String
Dim strBirthday As String
Dim iSum As Double = 0
Dim info As String = ""

'使用正则表达式验证是否为全数字或者最后是否为字母x
Dim rg As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("^\d{17}(\d|x)$")
Dim mc As System.Text.RegularExpressions.Match = rg.Match(cid)
If mc.Success = False Then
Return ""
End If


cid = cid.ToLower
cid = cid.Replace("x", "a")

'取得前2位的代码,并转换为对应的地址
'前两位的数字是aCity里地址的索引
strCity = aCity(Integer.Parse(cid.Substring(0, 2)))
If strCity = "" Then
Return "非法地址"
End If


Try
'建议用一个字符串把生日连接起来,然后再验证是否是DateTime格式。下面的代码
'Is Commented DateTime.Parse(cid.Substring(6, 4) & "-" & cid.Substring(10, 2) & "-" & cid.Substring(12, 2))

strBirthday = cid.Substring(6, 4) & "-" '身份证7-10位表示年份
strBirthday &= cid.Substring(10, 2) & "-" '同理,月份
strBirthday &= cid.Substring(12, 2) '哪一天出生
DateTime.Parse(strBirthday) '把字符串格式的生日转换成DateTime
Catch ex As Exception
Return "非法生日"
End Try '其实这里还可以加上个验证是否在最近的100年之内


'身份证是否合法的算法,不懂,还请楼主解释,不过可以直接拿来用?
Dim i As Integer
For i = 17 To 0 Step -1
iSum = iSum + (System.Math.Pow(2, i) Mod 11) * Integer.Parse(cid.Substring(17 - i, 1), System.Globalization.NumberStyles.HexNumber)
Next
If iSum Mod 11 <> 1 Then
Return "非法证号"
End If

'第17位是奇数表示性别为男,偶数为女?
Dim strSex As String
If (Integer.Parse(cid.Substring(16, 1)) Mod 2) = 1 Then
strSex = "男"
Else
strSex = "女"
End If

'最后返回一个含有居住省份,生日,性别的字符串
Return strCity & "," & strBirthday & "," & strSex
End Function
回复
xhm1027 2005-04-07
有注释吗??
回复
sg163 2005-04-06
很不错,不过有没有市的代码啊,如果有就最好了,呵呵。
回复
pshy 2005-04-06
我晕~~~~~~~~
回复
top1000 2005-04-06

做个标记
回复
beyond_zz 2005-04-06
强烈支持 VB.NET,要用实际行动证明VB的功能同样很强大,VB同样可以写出很漂亮的代码。
回复
相关推荐
发帖
VB
创建于2007-09-28

1.6w+

社区成员

VB技术相关讨论,主要为经典vb,即VB6.0
申请成为版主
帖子事件
创建了帖子
2005-03-29 04:41
社区公告
暂无公告