如何过滤用户输入的字符串

Vingsky 2004-11-20 04:21:37
我想过滤用户输入的字符,不想其中包含(<>?&*@#$%)等符号
...全文
193 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Vingsky 2004-11-20
  • 打赏
  • 举报
回复
呵……原来我不能给自己打分!其实这种状况我自己也应可得分呀!
Vingsky 2004-11-20
  • 打赏
  • 举报
回复
我可能没有把题目明确,大家都搞混了,我是要检查指定的字符串.
不过我已经搞定了:) 非常谢谢大家!
下面是我写的代码,有什么问题请各位大哥们多多指点

'--------------------------------------------------
' 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
最后我依据大家的代码,我学到一招限制输入字符的新方法
Gujianda 2004-11-20
  • 打赏
  • 举报
回复
我测试了一下,leolan(史留香)的办法比其他的快四倍(到底是红星二锅头:),当然其他人的灵活。
不知道能否给我几分,呵呵。
pice 2004-11-20
  • 打赏
  • 举报
回复
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

4103796 2004-11-20
  • 打赏
  • 举报
回复

Private Sub Text1_KeyPress(KeyAscii As Integer)
If InStr(1, "<>?&*@#$%", Str(KeyAscii)) > 0 Then
KeyAscii = 0
End If
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer) '限制只能输入数字
If InStr(1, "1234567890", Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If
End Sub
leolan 2004-11-20
  • 打赏
  • 举报
回复
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
gg137zeus 2004-11-20
  • 打赏
  • 举报
回复
按照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

End Sub
数码辣椒 2004-11-20
  • 打赏
  • 举报
回复
可以要keypress事件中进行过滤

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧