Click to Rate and Give Feedback
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
RegularExpressionValidator Class

Validates whether the value of an associated input control matches the pattern specified by a regular expression.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
Visual Basic (Declaration)
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class RegularExpressionValidator _
    Inherits BaseValidator
Visual Basic (Usage)
Dim instance As RegularExpressionValidator
C#
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class RegularExpressionValidator : BaseValidator
Visual C++
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction::InheritanceDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class RegularExpressionValidator : public BaseValidator
JScript
public class RegularExpressionValidator extends BaseValidator
ASP.NET
<asp:RegularExpressionValidator />

The RegularExpressionValidator control checks whether the value of an input control matches a pattern defined by a regular expression. This type of validation allows you to check for predictable sequences of characters, such as those in e-mail addresses, telephone numbers, and postal codes.

NoteNote:

Validation succeeds if the input control is empty. If a value is required for the associated input control, use a RequiredFieldValidator control in addition to the RegularExpressionValidator control.

Both server-side and client-side validation are performed unless the browser does not support client-side validation or client-side validation is explicitly disabled (by setting the EnableClientScript property to false).

The regular-expression validation implementation is slightly different on the client than on the server. On the client, JScript regular-expression syntax is used. On the server, System.Text.RegularExpressions..::.Regex syntax is used. Since JScript regular expression syntax is a subset of System.Text.RegularExpressions..::.Regex syntax, it is recommended that JScript regular-expression syntax be used in order to yield the same results on both the client and the server.

NoteNote:

When you use the RegularExpressionValidator control inside an UpdatePanel control, make sure that the validator control and the control it is associated with are in the same panel. For more information about using the UpdatePanel control for partial-page updates, see Partial-Page Rendering Overview.

For additional information about validation controls, see BaseValidator. For more information on regular expressions, see .NET Framework Regular Expressions.

Accessibility

The markup rendered by default for this control might not conform to accessibility standards such as the Web Content Accessibility Guidelines 1.0 (WCAG) priority 1 guidelines. For details about accessibility support for this control, see ASP.NET Controls and Accessibility.

TopicLocation
How to: Disable Validation for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Disable Validation for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Disable Validation for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Disable Validation for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Display Server Side Custom Validation Messages for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Display Server Side Custom Validation Messages for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Display Server Side Custom Validation Messages for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Display Server Side Custom Validation Messages for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Format Validation Error Messages for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Format Validation Error Messages for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Format Validation Error Messages for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Format Validation Error Messages for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Test Validity Programmatically for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Test Validity Programmatically for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Test Validity Programmatically for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Test Validity Programmatically for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Validate Against a Data Type for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against a Data Type for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against a Data Type for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Validate Against a Data Type for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Validate Against a Range of Values for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against a Range of Values for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against a Range of Values for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Validate Against a Range of Values for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Validate Against a Specific Value for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against a Specific Value for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against a Specific Value for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Validate Against a Specific Value for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Validate Against Patterns for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against Patterns for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against Patterns for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Validate Against Patterns for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Validate Against Values in a Database for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against Values in a Database for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against Values in a Database for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Validate Against Values in a Database for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Validate Required Entries for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Required Entries for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Required Entries for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Validate Required Entries for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Validate with a Custom Function for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate with a Custom Function for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate with a Custom Function for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Validate with a Custom Function for ASP.NET Server ControlsBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Validating User Input in a Web Forms PageBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Validating User Input in a Web Forms PageBuilding Applications with Visual Web Developer
Walkthrough: Validating User Input in a Web Forms PageBuilding ASP .NET Web Applications in Visual Studio

The following example demonstrates how to use the RegularExpressionValidator control to validate a five-digit postal code.

Security noteSecurity Note:

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

Visual Basic
<%@ Page Language="VB" AutoEventWireup="True" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  >
 <head>
    <title>RegularExpressionValidator Example</title>
<script runat="server">

       Sub ValidateBtn_Click(sender As Object, e As EventArgs) 

          If Page.IsValid Then 

             lblOutput.Text = "Page is Valid."

          Else 

             lblOutput.Text = "Page is InValid."

          End If

       End Sub

    </script>

 </head>
 <body>

    <h3>RegularExpressionValidator Example</h3>
    <br />

    <form id="form1" runat="server">

       <table style="background-color:#eeeeee; padding:10">
          <tr valign="top">
             <td colspan="3">
                <asp:Label ID="lblOutput" 
                     Text="Enter a 5-digit ZIP Code" 
                     runat="server"
                     AssociatedControlID="TextBox1"/>
             </td>
          </tr>

          <tr>
             <td colspan="3">
                <b>Personal Information</b>
             </td>
          </tr>
          <tr>
             <td align="right">
                Zip Code:
             </td>
             <td>
                <asp:TextBox id="TextBox1" 
                     runat="server"/>
             </td>
             <td>
                <asp:RegularExpressionValidator id="RegularExpressionValidator1" 
                     ControlToValidate="TextBox1"
                     ValidationExpression="\d{5}"
                     Display="Static"
                     ErrorMessage="Zip code must be 5 numeric digits"
                     EnableClientScript="False" 
                     runat="server"/>
             </td>
          </tr>
          <tr>
             <td></td>
             <td>
                <asp:Button text="Validate" 
                     OnClick="ValidateBtn_Click" 
                     runat="server" />
             </td>
             <td></td>
          </tr>
       </table>

    </form>

 </body>
 </html>

C#
<%@ Page Language="C#" AutoEventWireup="True" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  >
 <head>
    <title>RegularExpressionValidator Example</title>
<script runat="server">

       void ValidateBtn_Click(Object sender, EventArgs e) 
       {
          if (Page.IsValid) 
          {
             lblOutput.Text = "Page is Valid.";
          }
          else 
          {
             lblOutput.Text = "Page is InValid.";
          }
       }

    </script>

 </head>
 <body> 
    <form id="form1" runat="server">

    <h3>RegularExpressionValidator Example</h3>

       <table style="background-color:#eeeeee; padding:10">
          <tr valign="top">
             <td colspan="3">
                <asp:Label ID="lblOutput" 
                     Text="Enter a 5-digit ZIP Code" 
                     runat="server"
                     AssociatedControlID="TextBox1"/>
             </td>
          </tr>

          <tr>
             <td colspan="3">
                <b>Personal Information</b>
             </td>
          </tr>
          <tr>
             <td align="right">
                Zip Code:
             </td>
             <td>
                <asp:TextBox id="TextBox1" 
                     runat="server"/>
             </td>
             <td>
                <asp:RegularExpressionValidator id="RegularExpressionValidator1" 
                     ControlToValidate="TextBox1"
                     ValidationExpression="\d{5}"
                     Display="Static"
                     ErrorMessage="ZIP code must be 5 numeric digits"
                     EnableClientScript="False" 
                     runat="server"/>
             </td>
          </tr>
          <tr>
             <td></td>
             <td>
                <asp:Button text="Validate" 
                     OnClick="ValidateBtn_Click" 
                     runat="server" />
             </td>
             <td></td>
          </tr>
       </table>

    </form>
 </body>
 </html>

JScript
<%@ Page Language="JScript" AutoEventWireup="True" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  >
 <head>
    <title>RegularExpressionValidator Example</title>
<script runat="server">

       function ValidateBtn_Click(sender : Object, e : EventArgs) 
       {
          if (Page.IsValid) 
          {
             lblOutput.Text = "Page is Valid.";
          }
          else 
          {
             lblOutput.Text = "Page is InValid.";
          }
       }

    </script>

 </head>
 <body>

    <h3>RegularExpressionValidator Example</h3>
    <br />

    <form id="form1" runat="server">

       <table style="background-color:#eeeeee; padding:10">
          <tr valign="top">
             <td colspan="3">
                <asp:Label ID="lblOutput" 
                     Text="Enter a 5-digit ZIP Code" 
                     runat="server"
                     AssociatedControlID="TextBox1"/>
             </td>
          </tr>

          <tr>
             <td colspan="3">
                <b>Personal Information</b>
             </td>
          </tr>
          <tr>
             <td align="right">
                Zip Code:
             </td>
             <td>
                <asp:TextBox id="TextBox1" 
                     runat="server"/>
             </td>
             <td>
                <asp:RegularExpressionValidator id="RegularExpressionValidator1" 
                     ControlToValidate="TextBox1"
                     ValidationExpression="\d{5}"
                     Display="Static"
                     ErrorMessage="ZIP Code must be 5 numeric digits"
                     EnableClientScript="False" 
                     runat="server"/>
             </td>
          </tr>
          <tr>
             <td></td>
             <td>
                <asp:Button text="Validate" 
                     OnClick="ValidateBtn_Click" 
                     runat="server" />
             </td>
             <td></td>
          </tr>
       </table>

    </form>

 </body>
 </html>

System..::.Object
  System.Web.UI..::.Control
    System.Web.UI.WebControls..::.WebControl
      System.Web.UI.WebControls..::.Label
        System.Web.UI.WebControls..::.BaseValidator
          System.Web.UI.WebControls..::.RegularExpressionValidator
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
RegularExpression not matching      Sothryn   |   Edit   |   Show History

Using non-matching groupings, such as the suffix-matching, but exclude grouping regular expression:

(?=.{7,})(?=(.*[a-zA-Z]){1,})(?=(.*\d){1,})(?=(.*\W){1,})

will fail to match in the RegularExpressionValidator.

Workaround:

Placing it in a CustomValidator and handling it with a the RegularExpression classes works well.

Tags What's this?: Add a tag
Flag as ContentBug
More about RegularExpressionValidator      CSharpUniversity.com   |   Edit   |   Show History
To read more about the RegularExpressionValidator and see examples visit: http://www.csharpuniversity.com/2008/12/04/aspnet-validation-controls-regularexpressionvalidator/
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker