如何使文本框只能输入英文和数字和下划线
如何使文本框只能输入英文和数字和下划线,也就是邮箱名所支持的字符.
[解决办法]
建立一个文本框,在文本框的KeyPress写入以下代码.
'KeyAscii = 8 表示按下的是退格键.即清除键
'KeyAscii > 45 And KeyAscii < 58 表示按下的是 . 及数字键
'KeyAscii > 63 And KeyAscii < 90 表示按下的是 @ 及26个大写字符
'KeyAscii = 95 表示按下的是 _ 下划线
'KeyAscii > 96 And KeyAscii < 123 表示按下的是 26个小写字符
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not (KeyAscii = 8 Or (KeyAscii > 45 And KeyAscii < 58) Or (KeyAscii > 63 And KeyAscii < 90) Or KeyAscii = 95 Or (KeyAscii > 96 And KeyAscii < 123)) Then
KeyAscii = 0
End If
End Sub
[解决办法]
N年前的老问题了
'***************************************************************************************
'* 函数名称:ValiText
'* 功能: 限制文本框的输入字符
'* 输入: keyin一般是KeyAscii,Validatestring能在文本框出现代字符,Editable决定[Backspace]键是否可用
'* 变量: Validatelist , Keyout
'* 返回:ValiText
'***************************************************************************************
'在 TEXT 的KeyPress事件调用,参数Validatestring即是TEXT中想要出现的字符
'参数Editable即是否使用[Backspace]键
Function ValiText(keyin As Integer, Validatestring As String, Editable As Boolean) As Integer
Dim Validatelist As String
Dim Keyout As Integer
If Editable = True Then
Validatelist = UCase(Validatestring) & Chr(8)
Else
Validatelist = UCase(Validatestring)
End If
If InStr(1, Validatelist, UCase(Chr(keyin)), 1) > 0 Then
Keyout = keyin
Else
Keyout = 0
End If
ValiText = Keyout
End Function
调用例子:
Private Sub txtMoney_KeyPress(KeyAscii As Integer)
KeyAscii = ValiText(KeyAscii, "0123456789.", True)
End Sub
Private Sub txtID_KeyPress(KeyAscii As Integer)
KeyAscii = ValiText(KeyAscii, "0123456789-abcdefghijklmnopqrstuvwxyz", True)
End Sub