Validation Controls
Validation Controls
Validation controls are very useful while submitting data into a database table. These controls
prevent users from submitting the wrong type of data. There are six validation controls available
in the ASP.NET.
• RequiredFieldValidator
• RangeValidator
• CompareValidator
• RegularExpressionValidator
• CustomValidator
• ValidationSummary
By default, the validation controls perform validation on both the client (the browser) and the
server. These validation controls uses JavaScript to perform validation on client-side. Client side
validation is very fast because user gets immediate response whenever user enters an invalid
value into a form field. Sometimes browsers (mobile browsers) do not support javascript. If a
browser does not support JavaScript, then server side validation still perform. Validation controls
supports EnableClientScript property.You can disable client-side validation by assigning the
value False to EnableClientScript property of validation controls.
The validation controls inherit from the BaseValidator class therefore they inherit its
properties and methods. BaseValidator class is an abstract class.
Using Page.IsValid:
Validation controls supports an IsValid property that returns the value True if there is no
validation error. Also the Page.IsValid property returns the value True when the IsValid property
of the validation controls in a page returns the value True.
Display Property
All the validation controls supports a Display property that ensures how the validation
error message will be displayed.
Following are the three possible values:
• Static
• Dynamic
• None
By default, the Display property has the value Static.
RequiredFieldValidator
RequiredFieldValidator control is used to make an input control as a mandatory field or the input
field should not be empty. The other validation controls do not validate for an empty field. They
validate, when user enter the data.
For example, you can specify that users must fill password in a password TextBox
before submit the login form. You have to set two important properties when using the
RequiredFieldValdiator control.
ErrorMessage: This property is used to show the error message, if the user forget to enter the
data, then custom error message will appear.
You have to set the above two given property in all the ASP.NET validation control.
These two properties are common to all validation control except ValidationSummary control.
Example
<asp:TextBox ID="txtName"
runat="server"
Width="200px">
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server"
ControlToValidate="txtName"
ErrorMessage="Enter Name"
ForeColor="Red">
</asp:RequiredFieldValidator>
CompareValidator
CompareValidator control is used to perform three different types of validation tasks.
• To perform a data type check.
• To compare the value entered into a form field against a given fixed value.
• To compare the value of one form field against another field.
Important Properties
ValueToCompare: The fixed value against which to compare.
Type: The type of value for comparison. Given values are String, Integer, Double, Date, and
Currency.
if you don’t enter a value into the field being validated, then CompareValidator will not display
an error.
Example
<asp:TextBox D="txtConPassword"
runat="server"
Width="164px"
TextMode="Password">
</asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ForeColor="Red"
ControlToCompare="txtPassword"
ControlToValidate="txtConPassword"
ErrorMessage="Re-Enter Password">
</asp:CompareValidator>
RangeValidator
It confirms that user input data is within a specified range of values. The input value
should come between a certain minimum and maximum value otherwise it will give error.
Set the Type property according to input data while using the RangeValidator control.
By default, the Type property has the value String. If you want to specify the range of date of
birth, then set Type as Date. Along with ControlToValidate and ErrorMessage you have to set the
following property of RangeValidator control.
Important Properties
MinimumValue: The minimum value of the validation range.
MaximumValue: The maximum value of the validation range.
Type: Possible values are String, Integer, Double, Date, and Currency.
Example
<asp:TextBox ID="txtAge"
runat="server"
Width="90px">
</asp:TextBox>
<asp:RangeValidator ID="RangeValidator1"
runat="server" ForeColor="Red"
ControlToValidate="txtAge"
ErrorMessage="Enter age between 18 to 60"
MaximumValue="60" MinimumValue="18"
Type="Integer">
</asp:RangeValidator>
RegularExpressionValidator
The RegularExpressionValidator control performs its validation according to the
regular expression. You can check the predefined pattern, such as a phone number, postal code, e-
mail address, dates, and so on. The control uses the ValidationExpression property to set a valid
regular expression that is applied to the input data.
Example
<asp:TextBox ID="txtEmailID"
runat="server"
Width="200px">
</asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server"
ForeColor="Red"
ControlToValidate="txtEmailID"
ErrorMessage="Enter correct EmailID"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
ValidationSummary
This control summarizes the error messages from all validation controls on a Web page in
a single location. ValidationSummary control is useful when working with large forms.
If a user enters the wrong value in the field located at the end of the page, then the user
might never see the error message. ValidationSummary control can displays a list of errors at the
top of the form. It does not support ErrorMessage and ControlToValidate property. It
automatically displays the error message in the form of MessageBox or summary if error occurs
due to empty input field or wrong data.
Important Properties
DisplayMode: BulletList, List, and SingleParagraph
HeaderText: Display header text above the validation summary.
ShowMessageBox: It is used to display a popup alert box
ShowSummary: It hides the validation summary in the page.
By default ShowMessageBox property value is set False and ShowSummary property value is set
True. You can use these properties according to your need.
<asp:ValidationSummary ID="ValidationSummary1"
runat="server" >
</asp:ValidationSummary>
Example:
In the given example, We have not entered EmpID and EmpName and entered some
wrong data(Email, password), so you can see the error messages.