Regex Expression to check if there are any special

2019-08-01 07:06发布

I am trying to validate a user input for special characters using regular expression here is what i ave tried

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
  • this works fine for English
  • but when user enters some text in Chinese or Japanese alphabetic then this function fails.

Edit above solution worked for alphanumeric input but when user enters some Chinese or Japanese characters this function fails and returns false.

1条回答
别忘想泡老子
2楼-- · 2019-08-01 07:46

I solved this problem by putting all special characters in an array and checked user input for existence of special characters

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 

this was the most simple solution I could think of and it worked ..

查看更多
登录 后发表回答