VB 文本框输入数值,例如身份证码,在输入过程中如果少于15位或18位数,要有错误提示

来源:百度知道 编辑:UC知道 时间:2024/09/25 20:23:59
本人刚学VB 我想在文本框输入数值,如身份证码,在输入过程中如果少于15位或18位数,要有错误提示 ,我应用什么方法?? 有人给我写了以下代码,可是不行,有没有更好的方法,请帮忙看看,拜托

private sub text1_lostfocus()
if trim(text1.text)<>"" then
if len(trim(text1.text)<>15 or len(trim(text1.text)<>18 then
msgbox "请输入15位或18位身份证号!",48,"提示"
text1.setfocus
exit sub
end if
end if
end sub

因为他写的里面有好多错误所以不行,你改成我这样再试试
Private Sub text1_lostfocus()
If Trim(Text1.Text) = "" Then '如果文本框是空的就退出 sub
Exit Sub
ElseIf Len(Trim(Text1.Text)) = 15 Or Len(Trim(Text1.Text)) = 18 Then '如果输入的是15或18位的就提示正确
MsgBox "输入正确!", 48, "提示"
Else '否则提示错误
MsgBox "请输入15位或18位身份证号!", 48, "提示"
Text1.Text = ""
Text1.SetFocus
End If
End Sub

private sub text1_lostfocus()
if trim(text1.text)<>"" then
if len(trim(text1.text)=15 or len(trim(text1.text)=18 then
msgbox "输入z正确!",48,"提示"
text1.setfocus
else
msgbox "请输入15位或18位身份证号!",48,"提示"
else
msgbox "身份证不能为空!",48,"提示"
end if
end if
end sub