给你整理一下,自己看看!Private Sub Text_ID_LostFocus() 'ID为18位 If Len(Text_ID) = 18 Then Label_birthday.Caption = Mid(Text_ID.Text, 7, 4) + "-" + _ Mid(Text_ID.Text, 11, 2) + "-" + _ Mid(Text_ID.Text, 13, 2) If Int(CInt(Mid(Text_ID, 17, 1)) / 2) = CInt(Mid(Text_ID, 17, 1)) / 2 Then Label_sex.Caption = "女" else Label_sex.Caption = "男" end if '检查是否输入出错 if (Mid(Text_ID, 11, 2)) > 12 Or (Mid(Text_ID, 13, 2)) > 31 Then MsgBox "身份证输入有错误,请仔细检查!", vbExclamation end if ElseIf Len(Text_ID) = 15 Then 'ID为15位 Label_birthday.Caption = "19" + Mid(Text_ID.Text, 7, 2) + "-" +_ Mid(Text_ID.Text, 9, 2) + "-" + _ Mid(Text_ID.Text, 11, 2) If Int(CInt(Right(Text_ID, 1)) / 2) = CInt(Right(Text_ID, 1)) / 2 Then Label_sex.Caption = "女" Else Label_sex.Caption = "男" End If '检查是否输入出错 If (Mid(Text_ID, 9, 2)) > 12 Or (Mid(Text_ID, 11, 2)) > 31 Then MsgBox "身份证输入有错误,请仔细检查!", vbExclamation End If end if End Sub '分情况时没有分清楚,逻辑就容易出错
If Len(Text_ID) = 18 then if(Mid(Text_ID, 11, 2)) > 12 Or (Mid(Text_ID, 13, 2)) > 31 MsgBox "身份证输入有错误,请仔细检查!", vbExclamation End If End If
因为 AND 运算符优先于 OR 运算符,需将 OR 运算添加括号,即 If Len(Text_ID)=15 And (Mid(Text_ID, 9, 2))>12 Or (Mid(Text_ID, 11, 2))>31 Then 应为 If Len(Text_ID)=15 And ((Mid(Text_ID, 9, 2))>12 Or (Mid(Text_ID, 11, 2))>31) Then 而 If Len(Text_ID)=18 And (Mid(Text_ID, 11, 2))>12 Or (Mid(Text_ID, 13, 2))>31 Then 应为 If Len(Text_ID)=18 And ((Mid(Text_ID, 11, 2))>12 Or (Mid(Text_ID, 13, 2))>31) Then
你的代码逻辑上有问题。你标出的红色部分被嵌套在了“ElseIf Len(Text_ID) = 15 Then”个条件中,如果Text_ID的值长度为15,那么你那句红色语句中的"Len(Text_ID) = 18 "永远都不会成立,所以当长度为18时那句提示永远不会出现。另外,你可以使用IsDate(Label_birthday.Caption)来代替你的 If Len(Text_ID) = 15 And (Mid(Text_ID, 9, 2)) > 12 Or (Mid(Text_ID, 11, 2)) > 31 Then MsgBox "身份证输入有错误,请仔细检查!", vbExclamation End If If Len(Text_ID) = 18 And (Mid(Text_ID, 11, 2)) > 12 Or (Mid(Text_ID, 13, 2)) > 31 Then MsgBox "身份证输入有错误,请仔细检查!", vbExclamation End If它可以更准确地判断是否为正确的日期值。比如:2003-04-31,这个值在你的代码中会通过,但如果改为IsDate来判断就会出现提示了。更改你的代码如下(我还简化了你判断性别的代码):Private Sub Text_ID_LostFocus() If Len(Text_ID) = 18 Then '' 如果身份证号为18位 Label_birthday.Caption = Mid(Text_ID.Text, 7, 4) + "-" + Mid(Text_ID.Text, 11, 2) + "-" + Mid(Text_ID.Text, 13, 2) If Mid(Text_ID, 17, 1) Mod 2 Then Label_sex.Caption = "男" Else Label_sex.Caption = "女" End If ElseIf Len(Text_ID) = 15 Then '' 如果身份证号为15位 Label_birthday.Caption = "19" + Mid(Text_ID.Text, 7, 2) + "-" + Mid(Text_ID.Text, 9, 2) + "-" + Mid(Text_ID.Text, 11, 2) If Right(Text_ID, 1) Mod 2 Then Label_sex.Caption = "男" Else Label_sex.Caption = "女" End If End If If Not IsDate(Label_birthday.Caption) Then MsgBox "身份证输入有错误,请仔细检查!", vbExclamation End If End Sub 另外,如果想判断身份证号是否正确,你可以找一下相关的代码,好想连最后一位的校验位都可能进行检验的。
'ID为18位
If Len(Text_ID) = 18 Then
Label_birthday.Caption = Mid(Text_ID.Text, 7, 4) + "-" + _
Mid(Text_ID.Text, 11, 2) + "-" + _
Mid(Text_ID.Text, 13, 2)
If Int(CInt(Mid(Text_ID, 17, 1)) / 2) = CInt(Mid(Text_ID, 17, 1)) / 2 Then
Label_sex.Caption = "女"
else
Label_sex.Caption = "男"
end if
'检查是否输入出错
if (Mid(Text_ID, 11, 2)) > 12 Or (Mid(Text_ID, 13, 2)) > 31 Then
MsgBox "身份证输入有错误,请仔细检查!", vbExclamation
end if
ElseIf Len(Text_ID) = 15 Then 'ID为15位
Label_birthday.Caption = "19" + Mid(Text_ID.Text, 7, 2) + "-" +_
Mid(Text_ID.Text, 9, 2) + "-" + _
Mid(Text_ID.Text, 11, 2)
If Int(CInt(Right(Text_ID, 1)) / 2) = CInt(Right(Text_ID, 1)) / 2 Then
Label_sex.Caption = "女"
Else
Label_sex.Caption = "男"
End If
'检查是否输入出错
If (Mid(Text_ID, 9, 2)) > 12 Or (Mid(Text_ID, 11, 2)) > 31 Then
MsgBox "身份证输入有错误,请仔细检查!", vbExclamation
End If
end if
End Sub
'分情况时没有分清楚,逻辑就容易出错
if(Mid(Text_ID, 11, 2)) > 12 Or (Mid(Text_ID, 13, 2)) > 31
MsgBox "身份证输入有错误,请仔细检查!", vbExclamation
End If
End If
If Len(Text_ID)=15 And (Mid(Text_ID, 9, 2))>12 Or (Mid(Text_ID, 11, 2))>31 Then
应为
If Len(Text_ID)=15 And ((Mid(Text_ID, 9, 2))>12 Or (Mid(Text_ID, 11, 2))>31) Then
而
If Len(Text_ID)=18 And (Mid(Text_ID, 11, 2))>12 Or (Mid(Text_ID, 13, 2))>31 Then
应为
If Len(Text_ID)=18 And ((Mid(Text_ID, 11, 2))>12 Or (Mid(Text_ID, 13, 2))>31) Then
If Len(Text_ID) = 15 And (Mid(Text_ID, 9, 2)) > 12 Or (Mid(Text_ID, 11, 2)) > 31 Then
MsgBox "身份证输入有错误,请仔细检查!", vbExclamation
End If
If Len(Text_ID) = 18 And (Mid(Text_ID, 11, 2)) > 12 Or (Mid(Text_ID, 13, 2)) > 31 Then
MsgBox "身份证输入有错误,请仔细检查!", vbExclamation
End If它可以更准确地判断是否为正确的日期值。比如:2003-04-31,这个值在你的代码中会通过,但如果改为IsDate来判断就会出现提示了。更改你的代码如下(我还简化了你判断性别的代码):Private Sub Text_ID_LostFocus()
If Len(Text_ID) = 18 Then
'' 如果身份证号为18位
Label_birthday.Caption = Mid(Text_ID.Text, 7, 4) + "-" + Mid(Text_ID.Text, 11, 2) + "-" + Mid(Text_ID.Text, 13, 2)
If Mid(Text_ID, 17, 1) Mod 2 Then
Label_sex.Caption = "男"
Else
Label_sex.Caption = "女"
End If
ElseIf Len(Text_ID) = 15 Then
'' 如果身份证号为15位
Label_birthday.Caption = "19" + Mid(Text_ID.Text, 7, 2) + "-" + Mid(Text_ID.Text, 9, 2) + "-" + Mid(Text_ID.Text, 11, 2)
If Right(Text_ID, 1) Mod 2 Then
Label_sex.Caption = "男"
Else
Label_sex.Caption = "女"
End If
End If
If Not IsDate(Label_birthday.Caption) Then
MsgBox "身份证输入有错误,请仔细检查!", vbExclamation
End If
End Sub
另外,如果想判断身份证号是否正确,你可以找一下相关的代码,好想连最后一位的校验位都可能进行检验的。
我把红色段的代码放到整长为18的情况下,问题迎刃而解!!!!!