×

身份证严格验证(VB.NET源码)

Kalet Kalet 发表于2009-03-20 12:00:13 浏览242 评论0

抢沙发发表评论

身份证严格验证(VB.NET源码)



Public Class clsCheckId身份证严格验证(VB.NET源码)
    
Public dateId18 As Date
    
Public dateId15 As Date
    
Public errMessage As String
    
Public blnSex As Boolean

    
Public Function ChechPersonID(ByVal strIdNo As StringByVal blnIDlength As BooleanByVal blnWhichOccur As BooleanAs Boolean
        
'blnIDlength 为TRUE 是  为检测18身份证 否则  检测15位身份证       blnWhichOccur 为TRUE时 在change事件中发生 否则 在 leave事件中发生
        Select Case blnWhichOccur      'change事件中
            Case True    'change事件中发生
                If blnIDlength = True Then     '18
                    If strIdNo.Trim.Length = 0 Then Exit Function
                    strIdNo 
= Microsoft.VisualBasic.Left(strIdNo, 17)          '检测格式 是否含有不规范字符
                    If Not IsNumeric(strIdNo) Then
                        errMessage 
= "输入格式不正确"
                        
Exit Function
                        
Return False
                    
Else
                        
Return True
                    
End If
                
End If
                
If blnIDlength = False Then    ' 15位
                    If strIdNo.Trim.Length = 0 Then Exit Function
                    
'strIdNo = Microsoft.VisualBasic.Left(strIdNo, 15)
                    If Not IsNumeric(strIdNo) Then   '输入位数和格式
                        errMessage = "输入格式不正确"
                        
Exit Function
                        
Return False
                    
Else
                        
Return True
                    
End If
                
End If
            
Case False   'leave   事件              
                '选中18位
                Dim intSex As Integer 'sex
                If blnIDlength = True Then
                    intSex 
= CInt(Mid(strIdNo, strIdNo.Length - 11))
                    
If intSex = 1 Or intSex = 3 Or intSex = 5 Or intSex = 7 Or intSex = 9 Then
                        blnSex 
= True
                    
Else
                        blnSex 
= False
                    
End If
                    
'返回生日
                    If strIdNo.Length <> 18 Then
                        errMessage 
= "身份证位数不正确"
                        
Exit Function
                        
Return False
                    
Else
                        
Dim strYear As String = Mid(strIdNo, 74)
                        
Dim strMonth As String = Mid(strIdNo, 112)
                        
Dim strDay As String = Mid(strIdNo, 132)
                        
Dim isRunNian As Integer = (strYear Mod 4)
                        
If CInt(strYear) > 1901 And CInt(strYear) < 2050 And CInt(strMonth) <= 12 And CInt(strMonth) > 0 And CInt(strDay) > 0 Then
                            
Select Case isRunNian
                                
Case 0
                                    
If strMonth = 1 Or strMonth = 3 Or strMonth = 5 Or strMonth = 7 Or strMonth = 8 Or strMonth = 10 Or strMonth = 12 Then               '全部是错误的!!!!
                                        If strDay > 31 Then
                                            errMessage 
= "身份证日期错误"
                                            
Return False
                                        
Else
                                            dateId18 
= DateTime.Parse(System.String.Concat(strMonth + "," + strDay + "," + strYear))
                                            
Return True
                                        
End If
                                    
ElseIf strMonth = 4 Or strMonth = 6 Or strMonth = 9 Or strMonth = 11 Then             '全部是错误的!!!!
                                        If strDay > 30 Then
                                            errMessage 
= "身份证日期错误"
                                            
Return False
                                        
Else
                                            dateId18 
= DateTime.Parse(System.String.Concat(strMonth + "," + strDay + "," + strYear))
                                            
Return True
                                        
End If
                                    
Else : strMonth = 2 '闰年29
                                        If strDay > 29 Then
                                            errMessage 
= "身份证日期错误"
                                            
Return False
                                        
Else
                                            dateId18 
= DateTime.Parse(System.String.Concat(strMonth + "," + strDay + "," + strYear))
                                            
Return True
                                        
End If
                                    
End If
                                
Case Else
                                    
If strMonth = 1 Or strMonth = 3 Or strMonth = 5 Or strMonth = 7 Or strMonth = 8 Or strMonth = 10 Or strMonth = 12 Then               '全部是错误的!!!!
                                        If strDay > 31 Then
                                            errMessage 
= "身份证日期错误"
                                            
Return False
                                        
Else
                                            dateId18 
= DateTime.Parse(System.String.Concat(strMonth + "," + strDay + "," + strYear))
                                            
Return True
                                        
End If
                                    
ElseIf strMonth = 4 Or strMonth = 6 Or strMonth = 9 Or strMonth = 11 Then             '全部是错误的!!!!
                                        If strDay > 30 Then
                                            errMessage 
= "身份证日期错误"
                                            
Return False
                                        
Else
                                            dateId18 
= DateTime.Parse(System.String.Concat(strMonth + "," + strDay + "," + strYear))
                                            
Return True
                                        
End If
                                    
Else : strMonth = 2 '闰年29
                                        If strDay > 28 Then身份证严格验证(VB.NET源码)
                                            errMessage 
= "身份证日期错误"
                                            
Return False
                                        
Else
                                            dateId18 
= DateTime.Parse(System.String.Concat(strMonth + "," + strDay + "," + strYear))
                                            
Return True
                                        
End If
                                    
End If
                            
End Select
                        
Else                                      '不在正确年里面
                            errMessage = "身份证日期错误"
                        
End If
                    
End If
                
ElseIf blnIDlength = False Then       '15位身份证

                    intSex 
= Microsoft.VisualBasic.Right(strIdNo, 1)
                    
If intSex = 1 Or intSex = 3 Or intSex = 5 Or intSex = 7 Or intSex = 9 Then
                        blnSex 
= True
                    
Else
                        blnSex 
= False
                    
End If

                    
If strIdNo.Length <> 15 Then
                        errMessage 
= "身份证位数不正确"
                        
Exit Function
                        
Return False
                    
Else
                        
Dim strYear As String = Mid(strIdNo, 72)
                        
Dim strYear2 As String = "19" + strYear
                        
Dim strMonth As String = Mid(strIdNo, 92)
                        
Dim strDay As String = Mid(strIdNo, 112)
                        
Dim isRunNian As Integer = (CInt(strYear) Mod 4)
                        
If strYear2 > 1901 And strYear2 < 1999 And CInt(strMonth) <= 12 And CInt(strMonth) > 0 And CInt(strDay) > 0 Then
                            
Select Case isRunNian
                                
Case 0
                                    
If strMonth = 1 Or strMonth = 3 Or strMonth = 5 Or strMonth = 7 Or strMonth = 8 Or strMonth = 10 Or strMonth = 12 Then               '全部是错误的!!!!
                                        If strDay > 31 Then
                                            errMessage 
= "身份证日期错误"
                                            
Return False
                                        
Else
                                            dateId15 
= DateTime.Parse(System.String.Concat(strMonth + "," + strDay + "," + "19" + strYear))
                                            
Return True
                                        
End If

                                    
ElseIf strMonth = 4 Or strMonth = 6 Or strMonth = 9 Or strMonth = 11 Then             '全部是错误的!!!!
                                        If strDay > 30 Then
                                            errMessage 
= "身份证日期错误"
                                            
Return False
                                        
Else
                                            dateId15 
= DateTime.Parse(System.String.Concat(strMonth + "," + strDay + "," + "19" + strYear))
                                            
Return True
                                        
End If
                                    
Else
                                        
If strDay > 29 Then           '闰年29
                                            errMessage = "身份证日期错误"
                                            
Return False
                                        
Else
                                            dateId15 
= DateTime.Parse(System.String.Concat(strMonth + "," + strDay + "," + "19" + strYear))
                                            
Return True
                                        
End If
                                    
End If

                                
Case Else
                                    
If strMonth = 1 Or strMonth = 3 Or strMonth = 5 Or strMonth = 7 Or strMonth = 8 Or strMonth = 10 Or strMonth = 12 Then               '全部是错误的!!!!
                                        If strDay > 31 Then
                                            errMessage 
= "身份证日期错误"
                                            
Return False
                                        
Else
                                            dateId15 
= DateTime.Parse(System.String.Concat(strMonth + "," + strDay + "," + "19" + strYear))
                                            
Return True
                                        
End If

                                    
ElseIf strMonth = 4 Or strMonth = 6 Or strMonth = 9 Or strMonth = 11 Then             '全部是错误的!!!!
                                        If strDay > 30 Then
                                            errMessage 
= "身份证日期错误"
                                            
Return False
                                        
Else
                                            dateId15 
= DateTime.Parse(System.String.Concat(strMonth + "," + strDay + "," + "19" + strYear))
                                            
Return True
                                        
End If
                                    
ElseIf strMonth = 2 Then
                                        
If strDay > 28 Then
                                            errMessage 
= "身份证日期错误"
                                            
Return False
                                        
Else
                                            dateId15 
= DateTime.Parse(System.String.Concat(strMonth + "," + strDay + "," + "19" + strYear))
                                            
Return True
                                        
End If
                                    
End If

                            
End Select
                        
Else
                            errMessage 
= "身份证日期错误"
                        
End If
                    
End If
                
End If
        
End Select
    
End Function
End Class身份证严格验证(VB.NET源码)
 


群贤毕至

访客