Function IsValidIdCard(ByVal TS As String) As Boolean Dim intLen As Integer Dim str1 As String intLen = Len(TS) If Not (intLen = 15 Or intLen = 18) Then '无效 IsValidIdCard = False Exit Function End If If intLen = 15 Then str1 = TS Else str1 = Left(TS, 17) End If If Not IsNumeric(str1) Then IsValidIdCard = False Exit Function End If
Dim date1 As Date If intLen = 15 Then date1 = DateSerial(CInt(Mid(TS, 7, 2)), CInt(Mid(TS, 9, 2)), CInt(Mid(TS, 11, 2))) ElseIf intLen = 18 Then date1 = DateSerial(CInt(Mid(TS, 7, 4)), CInt(Mid(TS, 11, 2)), CInt(Mid(TS, 13, 2))) End If If date1 < #1/1/1885# Or date1 > Date Then '无效 IsValidIdCard = False Exit Function End If
(1)十七位数字本体码加权求和公式
S = Sum(Ai * Wi), i = 0, ... , 16 ,先对前17位数字的权求和
Ai:表示第i位置上的身份证号码数字值
Wi:表示第i位置上的加权因子
Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2(2)计算模
Y = mod(S, 11)(3)通过模得到对应的校验码
Y: 0 1 2 3 4 5 6 7 8 9 10
校验码: 1 0 X 9 8 7 6 5 4 3 2
这里有源代码下载
Dim intLen As Integer
Dim str1 As String
intLen = Len(TS)
If Not (intLen = 15 Or intLen = 18) Then '无效
IsValidIdCard = False
Exit Function
End If
If intLen = 15 Then
str1 = TS
Else
str1 = Left(TS, 17)
End If
If Not IsNumeric(str1) Then
IsValidIdCard = False
Exit Function
End If
Dim date1 As Date
If intLen = 15 Then
date1 = DateSerial(CInt(Mid(TS, 7, 2)), CInt(Mid(TS, 9, 2)), CInt(Mid(TS, 11, 2)))
ElseIf intLen = 18 Then
date1 = DateSerial(CInt(Mid(TS, 7, 4)), CInt(Mid(TS, 11, 2)), CInt(Mid(TS, 13, 2)))
End If
If date1 < #1/1/1885# Or date1 > Date Then '无效
IsValidIdCard = False
Exit Function
End If
IsValidIdCard = True '有效
End Function