RegExp to validate string
Scott wrote on 15 Dec 2002 in microsoft.public.scripting.
VBScript:
Quote:
> Hi,
> I need to do server-side string validation, to make sure the user has not
> typed any of the following characters into the input textbox:
> '
> "
> &
>%
> <
> I'm assuming I can do this with the RegExp object, but am open to other
> ways.
> I know I can do an IF INSTR...THEN evaluation for each invalid character,
> but was hoping for a more concise way of doing this.
General idea: "['&%><" & chr(34) & "]"
Tested:
<script language=vbscript>
Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches ' Create variable.
Set regEx = New RegExp ' Create a regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = True ' Set case insensitivity.
regEx.Global = True ' Set global applicability.
Set Matches = regEx.Execute(strng) ' Execute search.
For Each Match in Matches ' Iterate Matches collection.
RetStr = RetStr & "Match found at position "
RetStr = RetStr & Match.FirstIndex & ". Match Value is '"
RetStr = RetStr & Match.Value & "'." & vbCRLF
Next
If RetStr="" Then RetStr = "none found"
RegExpTest = RetStr
End Function
source = "bl'a'h&bl%ah<"
result = RegExpTest("['&%><" & chr(34) & "]", source)
msgbox(result)
source = "hello world"
result = RegExpTest("['&%><" & chr(34) & "]", source)
msgbox(result)
</script>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)