Private Sub Form_Load() Text1.MaxLength = 18 End SubPrivate Sub Text1_Validate(Cancel As Boolean) If Len(Text1.Text) < 3 Then Cancel = True End If If InStr(Text1.Text, "@") > 0 Then MsgBox "有@在" Else Cancel = True End If
End Sub
Private Sub Form_Load() Text1.MaxLength = 18 End SubPrivate Sub Text1_Validate(Cancel As Boolean) If Len(Text1.Text) <= 3 Then Cancel = True End If If InStr(Text1.Text, "@") > 0 Then MsgBox "有@在" Else Cancel = True End If
End Sub
在Validate事件中判断,如果少于3或者没有@就不能离开
TextBox 有 Maxlength 属性,定义最大长度,超过会忽略。用 Len 测试长度,少于某个长度提示错误,从新输入。@ 符号可以用 Mid 函数挨个测试,每次测试一个字符,直到找到一个 @ 为止。 Textbox1.Maxlength = 18在文本框的 LostFocus 事件中 If Len(Textbox1.Text) <= 3 Then MsgBox ("输入长度过短,请输入一个 3-18 位的字符串。") TextBox1.Text = "" End IfFor I = 1 To Len(TextBox1.Text) Test$ = Mid(TextBox.Text,I,1) If Test$ = "@" Then OkBoolean = True Exit For else OkBoolean = False End IF Next I If OkBoolean = False Then MsgBox ("在字符串中至少包含一个 @ ,请从新输入。") TextBox1.Text = "" End IF
Text1.MaxLength = 18
End SubPrivate Sub Text1_Validate(Cancel As Boolean)
If Len(Text1.Text) < 3 Then
Cancel = True
End If
If InStr(Text1.Text, "@") > 0 Then
MsgBox "有@在"
Else
Cancel = True
End If
End Sub
Text1.MaxLength = 18
End SubPrivate Sub Text1_Validate(Cancel As Boolean)
If Len(Text1.Text) <= 3 Then
Cancel = True
End If
If InStr(Text1.Text, "@") > 0 Then
MsgBox "有@在"
Else
Cancel = True
End If
End Sub
Textbox1.Maxlength = 18在文本框的 LostFocus 事件中
If Len(Textbox1.Text) <= 3 Then
MsgBox ("输入长度过短,请输入一个 3-18 位的字符串。")
TextBox1.Text = ""
End IfFor I = 1 To Len(TextBox1.Text)
Test$ = Mid(TextBox.Text,I,1)
If Test$ = "@" Then
OkBoolean = True
Exit For
else
OkBoolean = False
End IF
Next I
If OkBoolean = False Then
MsgBox ("在字符串中至少包含一个 @ ,请从新输入。")
TextBox1.Text = ""
End IF