Share via


RegularExpressionValidator Control (General Reference) 

Evaluates the value of an input control to determine whether it matches a pattern defined by a regular expression.

<asp:RegularExpressionValidator
    AccessKey="string"
    AssociatedControlID="string"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
        Inset|Outset"
    BorderWidth="size"
    ControlToValidate="string"
    CssClass="string"
    Display="None|Static|Dynamic"
    EnableClientScript="True|False"
    Enabled="True|False"
    EnableTheming="True|False"
    EnableViewState="True|False"
    ErrorMessage="string"
    Font-Bold="True|False"
    Font-Italic="True|False"
    Font-Names="string"
    Font-Overline="True|False"
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
                Large|X-Large|XX-Large"
        Font-Strikeout="True|False"
    Font-Underline="True|False"
    ForeColor="color name|#dddddd"
    Height="size"
    ID="string"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    runat="server"
    SetFocusOnError="True|False"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    Text="string"
    ToolTip="string"
    ValidationExpression="string"
    ValidationGroup="string"
    Visible="True|False"
    Width="size"
/>

Remarks

The RegularExpressionValidator control is used to determine 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 social security numbers, e-mail addresses, telephone numbers, postal codes, and so on.

NoteNote

If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to prevent the user from skipping an input 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 (the EnableClientScript property is set to false).

Use the ValidationExpression property to specify the regular expression used to validate an input control. The regular expression validation syntax is slightly different on the client than on the server. On the client, JScript regular expression syntax is used. On the server, Regex syntax is used. Because JScript regular expression syntax is a subset of Regex syntax, it is recommended that you use JScript regular expression syntax in order to yield the same results on both the client and the server.

For additional information on creating and formatting regular expressions, see .NET Framework Regular Expressions.

For detailed information on the RegularExpressionValidator control, see the RegularExpressionValidator class.

Example

The following code example demonstrates how to use the RegularExpressionValidator control to validate the value entered in a text box for a specific pattern. In this example, the pattern is a ZIP Code with five digits. The validation result is then displayed on the page.

<%@ 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 xmlns="http://www.w3.org/1999/xhtml" >
 <head>
    <title>RegularExpressionValidator Sample</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 Sample</h3>
    <p/>
 
    <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" 
                     Font-Names="Verdana" 
                     Font-Size="10pt" 
                     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"
                     EnableClientScript="false"
                     ErrorMessage="ZIP Code must be 5 numeric digits"
                     runat="server"/>
             </td>
          </tr>
          <tr>
             <td></td>
             <td>
                <asp:Button ID="Button1" text="Validate" 
                     OnClick="ValidateBtn_Click" 
                     runat="server"/>
             </td>
             <td></td>
          </tr>
       </table>
 
    </form>
 
 </body>
 </html>  
 <%@ 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 xmlns="http://www.w3.org/1999/xhtml" >
 <head>
    <title>RegularExpressionValidator Sample</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>
 
    <h3>RegularExpressionValidator Sample</h3>
    <p/>
 
    <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" 
                     Font-Names="Verdana" 
                     Font-Size="10pt" 
                     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"
                     EnableClientScript="false"
                     ErrorMessage="ZIP Code must be 5 numeric digits"
                     runat="server"/>
             </td>
          </tr>
          <tr>
             <td></td>
             <td>
                <asp:Button ID="Button1" text="Validate" 
                     OnClick="ValidateBtn_Click" 
                     runat="server"/>
             </td>
             <td></td>
          </tr>
       </table>
 
    </form>
 
 </body>
 </html>

See Also

Reference

RegularExpressionValidator

Other Resources

Validation Server Control Syntax
.NET Framework Regular Expressions