RegExp to validate string 
Author Message
 RegExp to validate string

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.

Thanks,

Scott



Thu, 02 Jun 2005 07:49:35 GMT  
 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)



Thu, 02 Jun 2005 08:13:56 GMT  
 RegExp to validate string
Hey, thanks a lot! That's what I was looking for.


Quote:
> Scott wrote on 15 Dec 2002 in microsoft.public.scripting.vbscript:

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



Thu, 02 Jun 2005 08:30:23 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. OT?: Need a good RegExp for validating e-mail adresses

2. Validate password strength (input) with regexp

3. Validating Email with regExp

4. validate a date string

5. RegExp String-almost-right

6. Convert string to RegExp pattern?

7. RegExp: how NOT to match string??

8. Regexp to match a subset of strings but exclude all of a certain character

9. RegExp: problems with comma-seperated string

10. zero-length string in RegExp

11. regexp matches unicode char in string

12. RegExp match of string with square bracket

 

 
Powered by phpBB® Forum Software