---
title: "Regular Expression in ASP.NET"  
description: "ASP.NET provides validation controls to help you check Web form data entries before the data is accepted and saved in the Database. Validation control"  
author: "AVADHESH PATEL"  
published: 2012-08-02  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/972/regular-expression-in-asp-dot-net  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 12 minutes  

---

# Regular Expression in ASP.NET

ASP.NET provides validation controls to help you check Web form data entries before the data is accepted and saved in the Database. Validation controls can be used to address the following questions.\

1. Did the user enter anything?\
2. Is the entry the appropriate kind of data (For example, Date of Birth should be a valid Date; Name should be a string etc.)?\
3. Is the data within a required range?(For example age cannot be greater than 100 years)\
The validation controls check the validity of data entered in associated server controls on the client before the page is posted back to the server. Most validity problems can be caught and corrected by the user without a round-trip to the server.

##### There are six controls which are available for performing validation. They are

1. RequiredFieldValidator

2. CompareValidator

3. RegularExpressionValidator

4. RangeValidator

5. CustomValidator

6. ValidationSummary

Create a new website project and you have an Empty default.aspx page. In your Solution Explorer [double click](https://www.mindstick.com/forum/156225/differentiate-between-google-adword-and-double-click) it and go to its [design view](https://answers.mindstick.com/qa/94814/design-view-is-not-getting-displayed-in-my-android-studio-what-should-i-do). Look for your toolbar on the left hand side of your window. In the Toolbar look for the Validation Section as depicted below

![Regular Expression in ASP.NET](https://www.mindstick.com/mindstickarticle/cb1ae170-021f-4fd0-bb53-2225744bb89f/images/43ed6d2a-2a99-47f2-8fd9-4bcbe6fa31e1.png)

1. RequiredFieldValidator

The **RequiredFieldValidator** control is used to make an input control a required field. With this control, the validation fails if the input value does not change from its initial value. By default, the initial value is an empty string ("").

**Note:** Leading and trailing spaces of the input value are removed before validation.

**Note:** The **InitialValue** property does not set the default value for the input control. It indicates the value that you do not want the user to enter in the input control.

##### Properties

| ## Property | ## Description |
| --- | --- |
| BackColor | The background color of the RequiredFieldValidator control |
| ControlToValidate | The id of the control to validate |
| Display | The display behavior for the validation control. Legal values are: - None (the control is not displayed. Used to show the error message only in the ValidationSummary control) - Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation. - Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation |
| EnableClientScript | A Boolean value that specifies whether client-side validation is enabled or not |
| Enabled | A Boolean value that specifies whether the validation control is enabled or not |
| ErrorMessage | The text to display in the ValidationSummary control when validation fails. **Note:** This text will also be displayed in the validation control if the Text property is not set |
| ForeColor | The foreground color of the control |
| id | A unique id for the control |
| InitialValue | Specifies the starting value of the input control. Default value is "" |
| IsValid | A Boolean value that indicates whether the control specified by ControlToValidate is determined to be valid |
| runat | Specifies that the control is a server control. Must be set to "server" |
| Text ToolTip | The message to display when validation fails Display for message on mouse over event |

##### Demo

Drag - Drop Lable, TextBox , Button and RequiredFieldValidator controls on webpage.

```
<body>    <form id="form1" runat="server">    <div>        <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Text="*" ControlToValidate="TextBox1" ToolTip="Provide some value in TextBox" Display="Dynamic"></asp:RequiredFieldValidator>        <asp:Button ID="Button1" runat="server" Text="Button" />    </div>    </form></body>
```

![Regular Expression in ASP.NET](https://www.mindstick.com/mindstickarticle/cb1ae170-021f-4fd0-bb53-2225744bb89f/images/362609cf-fd76-4d00-b799-bf93c771683f.png)

When we try click on Button ‘**Click’** without enter any string than ‘**RequiredFieldValidator’** fire

![Regular Expression in ASP.NET](https://www.mindstick.com/mindstickarticle/cb1ae170-021f-4fd0-bb53-2225744bb89f/images/01353661-c0dc-4658-a17c-1fc9ef3c3972.png)\

##### CompareValidator

The CompareValidator control is used to compare the value of one input control to the value of another input control or to a fixed value. For example compare email id or password.

##### Properties

##### Demo

Drag - Drop Lable, TextBox , Button and CompareValidator controls on webpage.

```
<body>    <form id="form1" runat="server">    <div>        <table>        <tr>            <td><asp:Label ID="Label1" runat="server" Text="Password"></asp:Label></td>            <td><asp:TextBox ID="TextBox1" runat="server" TextMode="Password"></asp:TextBox></td>        </tr>        <tr>            <td><asp:Label ID="Label2" runat="server" Text="Re-password"></asp:Label></td>            <td><asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>                <asp:CompareValidator ID="CompareValidator1" runat="server" Text="*" Display="Dynamic"                     ControlToCompare="TextBox1" ControlToValidate="TextBox2" ToolTip="Password not
match"></asp:CompareValidator>            </td>        </tr>        <tr><td></td><td><asp:Button ID="Button1" runat="server" Text="Click" /></td></tr>        </table>    </div>    </form></body>
```

![Regular Expression in ASP.NET](https://www.mindstick.com/mindstickarticle/cb1ae170-021f-4fd0-bb53-2225744bb89f/images/f9f95bc7-93aa-4d96-86c4-ea6b3a240217.png)

If we inter different – different values in TextBoxes “Password” and “Re-password” than CompareValidator fire

![Regular Expression in ASP.NET](https://www.mindstick.com/mindstickarticle/cb1ae170-021f-4fd0-bb53-2225744bb89f/images/e28567ea-2f0e-4211-b167-99071cc856eb.png)

3. RangeValidator

The RangeValidator control is used to check that the user enters an input value that falls between two values. It is possible to check ranges within numbers, dates, and characters.

##### Properties

| ## Property | ## Description |
| --- | --- |
| BackColor | The background color of the RangeValidator control |
| ControlToValidate | The id of the control to validate |
| Display | The display behavior for the validation control. Legal values are: - None (the control is not displayed. Used to show the error message only in the ValidationSummary control) - Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation. - Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation |
| EnableClientScript | A Boolean value that specifies whether client-side validation is enabled or not |
| Enabled | A Boolean value that specifies whether the validation control is enabled or not |
| ErrorMessage | The text to display in the ValidationSummary control when validation fails. **Note:** This text will also be displayed in the validation control if the Text property is not set |
| ForeColor | The foreground color of the control |
| id | A unique id for the control |
| IsValid | A Boolean value that indicates whether the control specified by ControlToValidate is determined to be valid |
| MaximumValue | Specifies the maximum value of the input control |
| MinimumValue | Specifies the minimum value of the input control |
| runat | Specifies that the control is a server control. Must be set to "server" |
| Type | Specifies the data type of the value to check. The types are: - Currency - Date - Double - Integer - String |
| Text | The message to display when validation fails |

##### Demo

Drag - Drop Lable, TextBox , Button and RangeValidator controls on webpage.

```
<body>    <form id="form1" runat="server">    <div>        <table>        <tr>            <td><asp:Label ID="Label1" runat="server" Text="Age"></asp:Label></td>            <td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>                <asp:RangeValidator ID="RangeValidator1" runat="server" Display="Dynamic" ControlToValidate="TextBox1"            MinimumValue="18" MaximumValue="28" Type="Integer" Text="age between 18-28
years"></asp:RangeValidator>            </td>        </tr>        <tr>            <td><asp:Label ID="Label2" runat="server" Text="DOB"></asp:Label></td>            <td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>                <asp:Label ID="Label3" runat="server" Text="dd/mm/yyyy"></asp:Label>                <asp:RangeValidator ID="RangeValidator2" runat="server" Display="Dynamic" ControlToValidate="TextBox2"            MinimumValue="01/07/1984" MaximumValue="01/07/1994" Type="Date" Text="date between
01/07/1984 to 01/07/1994"></asp:RangeValidator>            </td>              </tr>        <tr>            <td><asp:Label ID="Label4" runat="server" Text="Currency"></asp:Label></td>            <td><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>                <asp:RangeValidator ID="RangeValidator3" runat="server" Display="Dynamic" ControlToValidate="TextBox3"            MinimumValue="0.00" MaximumValue="9999999.00" Type="Currency" Text="e.g. 10000.00 or
100000"></asp:RangeValidator>        
   </td>              </tr>        <tr>        <td></td><td> <asp:Button ID="Button1" runat="server" Text="Click" /></td>        </tr>        </table>    </div>    </form></body>
```

![Regular Expression in ASP.NET](https://www.mindstick.com/mindstickarticle/cb1ae170-021f-4fd0-bb53-2225744bb89f/images/fd3166df-e72d-4201-bc02-71b418f94065.png)

If we inter wrong input in textboxes then **RangeValidator** display [error message](https://www.mindstick.com/forum/23174/error-message-the-page-you-are-requesting-cannot-be-served-because-of-the-extension-configuration).

![Regular Expression in ASP.NET](https://www.mindstick.com/mindstickarticle/cb1ae170-021f-4fd0-bb53-2225744bb89f/images/2e03ffce-b1fb-4cee-9a6a-2fb976d72d3b.png)

Right inputs

![Regular Expression in ASP.NET](https://www.mindstick.com/mindstickarticle/cb1ae170-021f-4fd0-bb53-2225744bb89f/images/99ad7aa6-87fc-4713-8197-94011f5ee2af.png)

4. RegularExpressionValidator

The RegularExpressionValidator control is used to ensure that an input value matches a specified pattern.

Note: Both server- and client-[side validation](https://www.mindstick.com/interview/33573/explain-client-and-server-side-validation) are performed unless the browser does not support client-side validation or the EnableClientScript property is set to false.

##### Properties

| ## Property | ## Description |
| --- | --- |
| BackColor | The [background color](https://www.mindstick.com/forum/18/split-background-color) of the RegularExpressionValidator control |
| ControlToValidate | The id of the control to validate |
| Display | The display behavior for the validation control. Legal values are: - None (the control is not displayed. Used to show the error message only in the ValidationSummary control) - Static (the control displays an error message if [validation fails](https://www.mindstick.com/forum/34291/how-to-raise-a-callback-when-asp-dot-net-mvc-client-validation-fails). Space is reserved on the page for the message even if the input passes validation. - Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation |
| EnableClientScript | A Boolean value that specifies whether client-side validation is enabled or not |
| Enabled | A Boolean value that specifies whether the validation control is enabled or not |
| ErrorMessage | The text to display in the ValidationSummary control when validation fails. **Note:** This text will also be displayed in the validation control if the [Text property](https://www.mindstick.com/forum/161/problem-in-getting-text-property-for-sender-object) is not set |
| ForeColor | The foreground color of the control |
| id | A unique id for the control |
| IsValid | A Boolean value that indicates whether the control specified by ControlToValidate is determined to be valid |
| runat | Specifies that the control is a server control. Must be set to "server" |
| BackColor | The background color of the RegularExpressionValidator control |
| Text | The message to display when validation fails |
| ValidationExpression | Specifies the expression used to [validate input](https://www.mindstick.com/forum/158278/how-to-use-the-pattern-attribute-to-validate-input-fields-against-a-specific-regular-expression) control. The expression validation syntax is different on the client than on the server. JScript is used on the client. On the server, the language you have specified is used |

##### Demo

Drag – Drop TextBox and RegularExpressionValidator controls on webpage.

```
<body>    <form id="form1" runat="server">    <div>        <table>        <tr>            <td>Name</td><td>                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"                    Display="Dynamic" ValidationExpression="^[a-zA-Z''-'\s]{1,40}$"                    ControlToValidate="TextBox1" ErrorMessage="only alphabets e.g. xyz"></asp:RegularExpressionValidator>                </td>            </tr>            <tr>            <td>Contact No.</td><td>                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>                <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server"                    Display="Dynamic" ValidationExpression="((\(\d{3}\)?)|(\d{3}-))?\d{3}-\d{4}"                    ControlToValidate="TextBox3" ErrorMessage="e.g. (555) 123-1234 "></asp:RegularExpressionValidator>                </td>            </tr>            <tr>            <td>Email Id</td><td>                <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>                <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server"                    Display="Dynamic"                                       ValidationExpression="^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$" ControlToValidate="TextBox4" ErrorMessage="e.g.
xyz@gmail.com"></asp:RegularExpressionValidator>                </td>            </tr>             <tr>            <td>Password</td><td>                <asp:TextBox ID="TextBox5" runat="server" TextMode="Password"></asp:TextBox>                <asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server"                    Display="Dynamic"                                       ValidationExpression="^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%&?]).{4,8}$"                    ControlToValidate="TextBox5" ErrorMessage="strong password e.g.Abc1#xyz (length 4-8)"></asp:RegularExpressionValidator>                </td>            </tr>        </table>    </div>    </form></body>
```

![Regular Expression in ASP.NET](https://www.mindstick.com/mindstickarticle/cb1ae170-021f-4fd0-bb53-2225744bb89f/images/9020a5b7-ff5a-4d9f-a6d8-77987bf792b9.png)

Notes- Here I used RegularExpressionValidator for-

**Name**: - Validates a name. Allows up to 40 uppercase and lowercase characters and a few special characters that are common to some names.

**Contact No**.:- Validates us contact number.

**Email Id: -** Validates an e-mail address.

**Password: -** Validates a strong password. Passwords will contain at least (1) upper case letter, at least (1) lower case letter, at least (1) number and (1) special character and at least (4-8) characters.

If we inter wrong input in textboxes then **RegularExpressionValidator** display error message.

![Regular Expression in ASP.NET](https://www.mindstick.com/mindstickarticle/cb1ae170-021f-4fd0-bb53-2225744bb89f/images/6b6d620d-828b-4294-b507-3cdab2c68017.png)

With right inputs

![Regular Expression in ASP.NET](https://www.mindstick.com/mindstickarticle/cb1ae170-021f-4fd0-bb53-2225744bb89f/images/bc045cc5-a258-4762-8e3a-cb84ecea86b1.png)

5. CustomValidator

The CustomValidator control allows you to write a method to handle the validation of the value entered.

##### Properties

| ## Property | ## Description |
| --- | --- |
| BackColor | The background color of the CustomValidator control |
| ClientValidationFunction | Specifies the name of the client-side validation script function to be executed. **Note:** The script must be in a language that the browser supports, such as VBScript or JScript With VBScript, the function must be in the form: Sub FunctionName (source, arguments) With JScript, the function must be in the form: Function FunctionName (source, arguments) |
| ControlToValidate | The id of the control to validate |
| Display | The display behavior for the validation control. Legal values are: - None (the control is not displayed. Used to show the error message only in the ValidationSummary control) - Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation. - Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation |
| EnableClientScript | A Boolean value that specifies whether client-side validation is enabled or not |
| Enabled | A Boolean value that specifies whether the validation control is enabled or not |
| ErrorMessage | The text to display in the ValidationSummary control when validation fails. **Note:** This text will also be displayed in the validation control if the Text property is not set |
| ForeColor | The foreground color of the control |
| id | A unique id for the control |
| IsValid | A Boolean value that indicates whether the control specified by ControlToValidate is determined to be valid |
| OnServerValidate | Specifies the name of the server-side validation script function to be executed |
| runat | Specifies that the control is a server control. Must be set to "server" |
| Text | The message to display when validation fails |

##### Demo

Drag - Drop TextBox, Button and CustomValidator controls on webpage.

```
<body>    <form id="form1" runat="server">    <div>    Enter
Value        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>        <asp:Button ID="Button1" runat="server" Text="Button" />        <asp:CustomValidator ID="CustomValidator1" runat="server" Text=""            ControlToValidate="TextBox1" onservervalidate="CustomValidator1_ServerValidate" errormessage="The text must be
exactly 8 characters long!"></asp:CustomValidator>           </div>    </form></body>
```

![Regular Expression in ASP.NET](https://www.mindstick.com/mindstickarticle/cb1ae170-021f-4fd0-bb53-2225744bb89f/images/4ab89215-7379-4baf-b547-109149a5feb7.png)

Note: - In this example, we will simply check the length of the string in the TextBox.

![Regular Expression in ASP.NET](https://www.mindstick.com/mindstickarticle/cb1ae170-021f-4fd0-bb53-2225744bb89f/images/0afbcde7-01a5-4d1f-ae09-0f5a52ee21d7.png)

6. ValidationSummary

The **ValidationSummary** control is used to display a summary of all validation errors occurred in a Web page.

The error message displayed in this control is specified by the **ErrorMessage** property of each validation control. If the **ErrorMessage** property of the validation control is not set, no error message is displayed for that validation control.

##### Properties

| ## Property | ## Description |
| --- | --- |
| DisplayMode | How to display the summary. Legal values are: - BulletList - List - SingleParagraph |
| EnableClientScript | A Boolean value that specifies whether client-side validation is enabled or not |
| Enabled | A Boolean value that specifies whether the validation control is enabled or not |
| ForeColor | The fore color of the control |
| HeaderText | A header in the ValidationSummary control |
| id | A unique id for the control |
| runat | Specifies that the control is a server control. Must be set to "server" |
| ShowMessageBox | A Boolean value that specifies whether the summary should be displayed in a message box or not |
| ShowSummary | A Boolean value that specifies whether the ValidationSummary control should be displayed or hidden |

##### Demo

Drag – Drop TextBox , Button and ValidationSummary controls on webpage.

```
<body>    <form id="form1" runat="server">    <div>        <table>        <tr>            <td>Name</td><td>                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"                    Display="Dynamic" ValidationExpression="^[a-zA-Z''-'\s]{1,40}$"                    ControlToValidate="TextBox1" ErrorMessage="Name accept only alphabets e.g.
xyz">*</asp:RegularExpressionValidator>                </td>            </tr>            <tr>            <td>Contact No.</td><td>                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>                <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server"                    Display="Dynamic" ValidationExpression="((\(\d{3}\)
?)|(\d{3}-))?\d{3}-\d{4}"                    ControlToValidate="TextBox3" ErrorMessage="Contact No. as (555) 123-1234
">*</asp:RegularExpressionValidator>                </td>            </tr>            <tr>            <td>Email Id</td><td>                <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>                <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server"                    Display="Dynamic"                                       ValidationExpression="^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"                    ControlToValidate="TextBox4" ErrorMessage="Email id as
xyz@gmail.com">*</asp:RegularExpressionValidator>                </td>            </tr>             <tr>            <td>Password</td><td>                <asp:TextBox ID="TextBox5" runat="server" TextMode="Password"></asp:TextBox>                <asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server"                    Display="Dynamic"                                       ValidationExpression="^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%&?]).{4,8}$"                    ControlToValidate="TextBox5" ErrorMessage="Strong password as Abc1#xyz
(length 4-8)">*</asp:RegularExpressionValidator>                </td>            </tr>            <tr>            <td></td><td>                <asp:Button ID="Button1" runat="server" Text="Submit" /></td>                </tr>        </table>    </div>    <asp:ValidationSummary ID="ValidationSummary1" runat="server" />    </form></body>
```

![Regular Expression in ASP.NET](https://www.mindstick.com/mindstickarticle/cb1ae170-021f-4fd0-bb53-2225744bb89f/images/0afbcde7-01a5-4d1f-ae09-0f5a52ee21d7.png)

Enter wrong inputs and press button ‘Submit’ then ValidationSummary error message display.

![Regular Expression in ASP.NET](https://www.mindstick.com/mindstickarticle/cb1ae170-021f-4fd0-bb53-2225744bb89f/images/167205d9-a17d-4eed-8311-79f027ed889d.png)

---

Original Source: https://www.mindstick.com/articles/972/regular-expression-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
