我正在尝试使用正则表达式验证特殊字符的用户输入,这是我试过的
Function IsValidName(strData As String) As Boolean
Dim RE As Object, REMatches As Object
Set RE = CreateObject("vbscript.regexp")
With RE
.MultiLine = False
.Global = True
.IgnoreCase = True
.Pattern = "[^a-zA-Z0-9]"
End With
IsValidName = Not RE.Test(strData)
End Function
修改 上述解决方案适用于字母数字输入,但当用户输入一些中文或日文字符时,此功能失败并返回false。
答案 0 :(得分:1)
我通过将所有特殊字符放入数组并检查用户输入是否存在特殊字符
来解决此问题Dim special_charArr() As String
Dim special_char As String
Dim charIndex As Integer
Dim TotalIndex As Integer
charIndex = 0
TotalIndex = 0
special_char = "!,@,#,$,%,^,&,*,+,/,\,;,:"
special_charArr() = Split(special_char,",")
For Each key in special_charArr
charIndex = Instr(UserInput,key)
TotalIndex = TotalIndex + charIndex
Next
If TotalIndex > 0 Then
MsgBox "Invalid Input"
Else
MsgBox "Valid Value"
End If
这是我能想到的最简单的解决方案,而且有效。