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 it and go to its design view. Look for your
toolbar on the left hand side of your window. In the Toolbar look for the
Validation Section as depicted below

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>
|

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

2.
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
|
Property
|
Description
|
|
BackColor
|
The background color of the CompareValidator control
|
|
ControlToCompare
|
The name of the control to compare with
|
|
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
|
|
Operator
|
The type of comparison to perform. The operators are:
- Equal
- GreaterThan
- GreaterThanEqual
- LessThan
- LessThanEqual
- NotEqual
- DataTypeCheck
|
|
runat
|
Specifies that the control is a server control. Must be set to "server"
|
|
Text
|
The message to display when validation fails
|
|
Type
|
Specifies the data type of the values to compare. The types are:
- Currency
- Date
- Double
- Integer
- String
|
|
ValueToCompare
ToolTip
|
A specified value to compare with
Display for message on mouse over event
|
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>
|

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

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>
|

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

Right inputs

4.
RegularExpressionValidator
The RegularExpressionValidator
control is used to ensure that an input value matches a specified pattern.
Note: Both server- and client-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 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. 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
|
|
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 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>
|

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.

With right inputs

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>
|

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

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>
|

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

|