'--------------------------------------------------
' Function: CheckStringInvalid
' Purpose: 检查字符串的有效性
' Input:
' [in] CheckedString -- 接受(被)检查的字符串表达式。
' [out] InvalidCharaters -- 无效的字符串表达式。
' Output: True: 被检查的字符串没含有无效字符
' False:被检查的字符串含有无效字符
' Content: None
' Information:
' 编写: 本人 2004/11/20 说明:
' 修改: 本人 2004/11/20 说明:
'--------------------------------------------------
Function CheckString(CheckedString As String, InvalidCharaters As String) As Boolean
Dim RetVal As Long, str1 As String, str2 As String, intLength As Integer, i As Integer
intLength = Len(InvalidCharaters)
For i = 1 To intLength '用InvalidCharaters中每个字符单独历遍CheckedString
RetVal = InStr(1, CheckedString, Mid(InvalidCharaters, i, 1), vbTextCompare)
If RetVal Then '如找到退出循环
Exit For
End If
Next i
If RetVal Then '如找到为 True
CheckString = False
Else '未找到为 Fasle
CheckString = True
End If
End Function
最后我依据大家的代码,我学到一招限制输入字符的新方法
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim sTemplate As String
sTemplate = "!@#$%^&*()_+-=" '用來存放不接受的字元
If InStr(1, sTemplate, Chr(KeyAscii)) > 0 Then
KeyAscii = 0
End If
End Sub
Private Const mc_strInvalidCharaters As String = "(<>?&*@#$%)"
Private Sub Text1_KeyPress(KeyAscii As Integer)
If InStr(1, mc_strInvalidCharaters, Chr$(KeyAscii), vbTextCompare) > 0 Then
KeyAscii = 0
End If
End Sub
按照ASCII值判断一下
Private Sub Txt_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKey0 To vbKey9
Case vbKeyBack
Case vbkeya to vbkeyz
Case vbkeyA to vbkeyZ
Case Else
KeyAscii = 0
End Select