判断身份证号码的正确性源码
根据〖中华人民共和国国家标准 GB 11643-1999〗中有关公民身份号码的规定,
'公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成。排列顺序从左至右依次为: '六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。 '地址码表示编码对象常住户口所在县(市、旗、区)的行政区划代码。生日期码表示编码对象出生的 '年、月、日,其中年份用四位数字表示,年、月、日之间不用分隔符。顺序码表示同一地址码所标识的 '区域范围内,对同年、月、日出生的人员编定的顺序号。 '顺序码的奇数分给男性,偶数分给女性。 '校验码是根据前面十七位数字码,按照ISO 7064:1983.MOD 11-2校验码计算出来的检验码。下面举例说明该计算方法。 '某男性公民身份号码本体码为34052419800101001,首先按照公式⑴计算: '∑(ai×Wi)(mod 11)……………………………………(1) '公式(1)中: 'i----表示号码字符从由至左包括校验码在内的位置序号; 'ai----表示第i位置上的号码字符值; 'Wi----示第i位置上的加权因子,其数值依据公司Wi=2(n-1)(mod 11)计算得出。(这是一个常数数组) 'i 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 ' 'ai 3 4 0 5 2 4 1 9 8 0 0 1 0 1 0 0 1 a1 ' 'Wi 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 1 ' 'ai×Wi 21 36 0 25 16 16 2 9 48 0 0 9 0 5 0 0 2 a1 ' '根据公式(1)进行计算: '∑(ai×WI) = (21 + 36 + 0 + 25 + 16 + 16 + 2 + 9 + 48 + 0 + 0 + 9 + 0 + 5 + 0 + 0 + 2) = 189 '189 ÷ 11 = 17 + 2 / 11 '∑(ai×Wi)(mod 11) = 2 '然后根据计算的结果,从下面的表中查出相应的校验码,其中X表示计算结果为10: '∑(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 '根据上表,查出计算结果为2的校验码为所以该人员的公民身份号码应该为 34052419800101001X。 Option Explicit Dim Wi(1 To 18) As Integer '校验码 Private Function SetWi() Wi(1) = 7 Wi(2) = 9 Wi(3) = 10 Wi(4) = 5 Wi(5) = 8 Wi(6) = 4 Wi(7) = 2 Wi(8) = 1 Wi(9) = 6 Wi(10) = 3 Wi(11) = 7 Wi(12) = 9 Wi(13) = 10 Wi(14) = 5 Wi(15) = 8 Wi(16) = 4 Wi(17) = 2 Wi(18) = 1 End Function Public Function CheckCIDC15(ByVal StrID15 As String) If Not IsNumeric(StrID15) Then MsgBox "15位身份证号码输入有误!" & vbCrLf & "有非数字出现!" CheckCIDC15 = False Exit Function End If If Val(Mid(StrID15, 9, 2)) < 1 Or Val(Mid(StrID15, 9, 2)) > 12 Then MsgBox "身份证号码输入有误!" & vbCrLf & "月份不正确!" CheckCIDC15 = False Exit Function End If If Val(Mid(StrID15, 11, 2)) < 1 Or Val(Mid(StrID15, 11, 2)) > 31 Then MsgBox "身份证号码输入有误!" & vbCrLf & "日期不正确!" CheckCIDC15 = False Exit Function Else If (Val(Mid(StrID15, 9, 2)) = 4 Or Val(Mid(StrID15, 9, 2)) = 6 Or Val(Mid(StrID15, 9, 2)) = 9 Or Val(Mid(StrID15, 9, 2)) = 11) And Val(Mid(StrID15, 11, 2)) = 31 Then MsgBox "身份证号码输入有误!" & vbCrLf & "月份和日期不匹配" CheckCIDC15 = False Exit Function ElseIf Val(Mid(StrID15, 9, 2)) = 2 And (Val(Mid(StrID15, 11, 2)) = 30 Or Val(Mid(StrID15, 11, 2)) = 31) Then MsgBox "身份证号码输入有误!" & vbCrLf & "2月份没有" & Val(Mid(StrID15, 11, 2)) & "天" CheckCIDC15 = False Exit Function End If End If CheckCIDC15 = True End Function Public Function CheckCIDC18(ByVal StrID18 As String) Dim StrID17 As String, AiWi As Integer, num As Integer, A18 As String SetWi If Not IsNumeric(Left(StrID18, 17)) Then MsgBox "身份证号码输入有误!" CheckCIDC18 = False Exit Function End If If Val(Mid(StrID18, 11, 2)) < 1 Or Val(Mid(StrID18, 11, 2)) > 12 Then MsgBox "身份证号码输入有误!" & vbCrLf & "月份不正确!" CheckCIDC18 = False Exit Function End If If Val(Mid(StrID18, 13, 2)) < 1 Or Val(Mid(StrID18, 13, 2)) > 31 Then MsgBox "身份证号码输入有误!" & vbCrLf & "日期不正确!" CheckCIDC18 = False Exit Function Else If (Val(Mid(StrID18, 11, 2)) = 4 Or Val(Mid(StrID18, 11, 2)) = 6 Or Val(Mid(StrID18, 11, 2)) = 9 Or Val(Mid(StrID18, 11, 2)) = 11) And Val(Mid(StrID18, 13, 2)) = 31 Then MsgBox "身份证号码输入有误!" & vbCrLf & "月份和日期不匹配" CheckCIDC18 = False Exit Function ElseIf Val(Mid(StrID18, 11, 2)) = 2 And (Val(Mid(StrID18, 13, 2)) = 30 Or Val(Mid(StrID18, 13, 2)) = 31) Then MsgBox "身份证号码输入有误!" & vbCrLf & "2月份没有" & Val(Mid(StrID18, 13, 2)) & "天" CheckCIDC18 = False Exit Function End If End If StrID17 = Left(StrID18, 17) AiWi = 0 For num = 1 To 17 AiWi = AiWi + Val(Mid(StrID17, num, 1)) * Wi(num) Next num Select Case AiWi Mod 11 Case 0 A18 = "1" Case 1 A18 = "0" Case 2 A18 = "X" Case 3 A18 = "9" Case 4 A18 = "8" Case 5 A18 = "7" Case 6 A18 = "6" Case 7 A18 = "5" Case 8 A18 = "4" Case 9 A18 = "3" Case 10 A18 = "2" End Select If A18 <> Right(StrID18, 1) Then MsgBox "身份证号码输入有误!" & vbCrLf & "尾数校验码不正确" CheckCIDC18 = False Exit Function End If CheckCIDC18 = True End Function Public Function CIDC15To18(ByVal StrID15 As String) SetWi Dim StrID17 As String, StrID18 As String, num As Integer, AiWi As Integer If Not IsNumeric(StrID15) Then MsgBox "15位身份证号码输入有误!" & vbCrLf & "有非数字出现!" Exit Function End If If Val(Mid(StrID15, 9, 2)) < 1 Or Val(Mid(StrID15, 9, 2)) > 12 Then MsgBox "身份证号码输入有误!" & vbCrLf & "月份不正确!" Exit Function End If If Val(Mid(StrID15, 11, 2)) < 1 Or Val(Mid(StrID15, 11, 2)) > 31 Then MsgBox "身份证号码输入有误!" & vbCrLf & "日期不正确!" Exit Function Else If (Val(Mid(StrID15, 9, 2)) = 4 Or Val(Mid(StrID15, 9, 2)) = 6 Or Val(Mid(StrID15, 9, 2)) = 9 Or Val(Mid(StrID15, 9, 2)) = 11) And Val(Mid(StrID15, 11, 2)) = 31 Then MsgBox "身份证号码输入有误!" & vbCrLf & "月份和日期不匹配" Exit Function ElseIf Val(Mid(StrID15, 9, 2)) = 2 And (Val(Mid(StrID15, 11, 2)) = 30 Or Val(Mid(StrID15, 11, 2)) = 31) Then MsgBox "身份证号码输入有误!" & vbCrLf & "2月份没有" & Val(Mid(StrID15, 11, 2)) & "天" Exit Function End If End If StrID17 = Left(StrID15, 6) & "19" & Right(StrID15, 9) AiWi = 0 For num = 1 To 17 AiWi = AiWi + Val(Mid(StrID17, num, 1)) * Wi(num) Next num Select Case AiWi Mod 11 Case 0 StrID18 = StrID17 & "1" Case 1 StrID18 = StrID17 & "0" Case 2 StrID18 = StrID17 & "X" Case 3 StrID18 = StrID17 & "9" Case 4 StrID18 = StrID17 & "8" Case 5 StrID18 = StrID17 & "7" Case 6 StrID18 = StrID17 & "6" Case 7 StrID18 = StrID17 & "5" Case 8 StrID18 = StrID17 & "4" Case 9 StrID18 = StrID17 & "3" Case 10 StrID18 = StrID17 & "2" End Select CIDC15To18 = StrID18 End Function |