如题,就像注册账号时,账号名只能输入英文,密码必需大于8位,且必需英文或数字
Text1.IMEMode = 3就不能输入中文了,但可以粘
Option Explicit
Private Sub Command1_Click()
Dim i As Integer
Dim UCharUserName As String
Dim UCharPassword As String
If Text1.Text <> "" Then '检查用户名
UCharUserName = UCase$(Text1.Text)
For i = 1 To Len(UCharUserName)
If Mid$(UCharUserName, i, 1) >= "A" And Mid$(UCharUserName, i, 1) <= "Z" Then
'输入正确
Else
MsgBox "输入错误,请输入英文字母"
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
Text1.SetFocus
Exit Sub
End If
Next
Else
MsgBox "请输入用户名"
Exit Sub
End If
If Text2.Text <> "" Then '检查密码
UCharPassword = UCase$(Text2.Text)
For i = 1 To Len(UCharPassword)
If Mid$(UCharPassword, i, 1) >= "A" And Mid$(UCharPassword, i, 1) <= "Z" Or IsNumeric(Mid$(UCharPassword, i, 1)) Then
'输入正确
Else
MsgBox "输入错误,请输入英文或数字"
Text2.SelStart = 0
Text2.SelLength = Len(Text2.Text)
Text2.SetFocus
Exit Sub
End If
Next
Else
MsgBox "请输入密码"
Exit Sub
End If
MsgBox "输入正确"
End Sub
用keydown 判断 ascii 码如果ascii 码在 asc("a")到asc("z")和asc("A")到asc("Z")之间就是英文了
判断中文的就更加简单了只要判断 ascii 码是否负数就行了
(if sgn(keycode)=-1[判断是否为负数] then keycode=0 [限制输入])
至于判断数字的 判断ascii 是否在 asc("1")到asc("0")就行了
但是要记住一定要在keydown 处判断并用 keycode=0 限制输入
Option Explicit
Private Sub Command1_Click()
Dim i As Integer
Dim UCharUserName As String
Dim UCharPassword As String
If Text1.Text <> "" Then '检查用户名
UCharUserName = UCase$(Text1.Text) '将文本框里的字符转换成大写
For i = 1 To Len(UCharUserName)
If Mid$(UCharUserName, i, 1) >= "A" And Mid$(UCharUserName, i, 1) <= "Z" Then '判断是否是A到Z之间的英文字母
'输入正确
Else
MsgBox "输入错误,请输入英文字母"
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
Text1.SetFocus
Exit Sub
End If
Next
Else
MsgBox "请输入用户名"
Exit Sub
End If
If Text2.Text <> "" Then '检查密码
UCharPassword = UCase$(Text2.Text) '将文本框里的字符转大字
For i = 1 To Len(UCharPassword)
If Mid$(UCharPassword, i, 1) >= "A" And Mid$(UCharPassword, i, 1) <= "Z" Or IsNumeric(Mid$(UCharPassword, i, 1)) Then '判断是否为A到Z之间的字母或数字
'输入正确
Else
MsgBox "输入错误,请输入英文或数字"
Text2.SelStart = 0
Text2.SelLength = Len(Text2.Text)
Text2.SetFocus
Exit Sub
End If
Next
Else
MsgBox "请输入密码"
Exit Sub
End If
MsgBox "输入正确"
End Sub