This documentation is archived and is not being maintained.

RegularExpressionValidator.ValidationExpression Property

Gets or sets the regular expression that determines the pattern used to validate a field.

[Visual Basic]
Public Property ValidationExpression As String
[C#]
public string ValidationExpression {get; set;}
[C++]
public: __property String* get_ValidationExpression();
public: __property void set_ValidationExpression(String*);
[JScript]
public function get ValidationExpression() : String;
public function set ValidationExpression(String);

Property Value

A string that specifies the regular expression used to validate a field for format. The default is String.Empty.

Exceptions

Exception Type Condition
HttpException The regular expression is not properly formed.

Remarks

Use this property to specify the pattern used to check for predictable sequences of characters, such as those in social security numbers, e-mail addresses, telephone numbers, and postal codes.

Example

[Visual Basic, C#, JScript] The following example demonstrates how to use the ValidationExpression property to validate a field with five numeric digits.

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

 <html>
 <head>

    <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>
    <p>
 
    <form runat="server">
 
       <table bgcolor="#eeeeee" cellpadding="10">
          <tr valign="top">
             <td colspan="3">
                <asp:Label ID="lblOutput" 
                     Text="Enter a 5 digit zip code" 
                     runat="server"/>
             </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" %>

 <html>
 <head>

    <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 Example</h3>
    <p>
 
    <form runat="server">
 
       <table bgcolor="#eeeeee" cellpadding="10">
          <tr valign="top">
             <td colspan="3">
                <asp:Label ID="lblOutput" 
                     Text="Enter a 5 digit zip code" 
                     runat="server"/>
             </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" %>

 <html>
 <head>

    <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>
    <p>
 
    <form runat="server">
 
       <table bgcolor="#eeeeee" cellpadding="10">
          <tr valign="top">
             <td colspan="3">
                <asp:Label ID="lblOutput" 
                     Text="Enter a 5 digit zip code" 
                     runat="server"/>
             </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++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

RegularExpressionValidator Class | RegularExpressionValidator Members | System.Web.UI.WebControls Namespace

Show: