.NET Regular Expression in Code Behind

Most the time when we’re using regular expressions we’re doing it with in our aspx page checking against a text box or some other control.

example

'

<asp:RegularExpressionValidator ID="regfirstName"
 runat="server" ErrorMessage="This expression does not validate."
  ControlToValidate="txtFirstName" 
ValidationExpression="^[a-zA-Z0-9'.\s]{1,40}$" />

'

When working with Query Strings you’ll need to compare the QueryString against a regular expression in the code behine, this is done by using the regex class from the System.Text.RegularExpressions namespace.

example

'

If Regex.IsMatch(Request.QueryString("region"), "^[a-zA-Z0-9\s\-]+$") Then

                Else

                End If

'

btw great site to help and/or get commonly user regular expressions is regexlib.com

Technorati Tags: , , , , ,

     

Leave a Reply