1

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.

Community
  • 1
  • 1
AddyProg
  • 2,960
  • 13
  • 59
  • 110
  • So you want to allow any chinese, japanese or english alphanumerics? – brettdj Dec 23 '14 at 06:24
  • This seems like the [XY problem](http://meta.stackexchange.com/a/66378/274165), in that what you want to do is probably not well-expressed by what you're asking for. Validating that usernames are not going to blow up your database or contribute to XSS down the line is something a little different from checking that they're alphanumeric, and regexen may or may not be the best tool for that. – Nathan Tuggy Dec 23 '14 at 06:36
  • What is a "special character"? – Toto Dec 23 '14 at 10:09
  • @NathanTuggy ; yeah you are rite.. regex didn't worked, so i placed all invalid characters in an array and checked user input for each special character in that array. and showed invalid input message if user input contains any of these special characters. – AddyProg Dec 24 '14 at 04:32
  • If you could rephrase your question to include the original motivation, then add the answer you ended up with, that might help someone in the future. – Nathan Tuggy Dec 24 '14 at 04:37

1 Answers1

1

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 ..

AddyProg
  • 2,960
  • 13
  • 59
  • 110