It is always good to repeat some of the business critical validations on server side
as a precaution. From 1.x days, asp.net is packed with a list of validator
control that assists in easy validations in asp.net applications.These validation controls may or may not satisfy all our
needs in the project. If you are an asp.net developer for quite sometime then
you will have a basic idea on these validation controls. Not every time we can
use these inbuilt validation controls as it is to serve our needs. This article
will help us to use the asp.net validation control to the maximum extent to
serve our project needs.
Validator control will fire on blur
By default, when we use asp.net validator control it
will fire the validations on blur of the control. To provide a good user
experience it is good if the validation controls are fired whenever submit
button is clicked.
Read my previous article on Restrict
Asp.Net Validator controls to Fire on Page Submit that speaks about
restricting validator controls to fire only on submit. This approach will be
good when the number of input controls are less.
Validating DropDownList with RequiredField Validator
We use RequiredField validator to validate an input
control to be mandatory. When we use dropdownlist control it is not enough if we
set the ControlToValidate property to the dropdown id. To make the RequiredField
validator to work for DropDownList we need to set the InitialValue property to
the default value of the DropDownList for doing mandatory check with
RequiredField validator.
Refer the below code,
<asp:DropDownList ID="ddlRank" runat="server">
<asp:ListItem>Select a
Rank</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1"
InitialValue="Select a Rank" runat="server"
ControlToValidate="ddlRank"
ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
Validation Group in ASP.Net 2.0
The ASP.Net validation controls will fire for any
postback caused by any of the controls on the page. To restrict this behavior
for a control, say a Button, we need to set the CausesValidation property to
false. Setting this property to false means we can’t use any validation control
to validate the input for that button till ASP.Net 1.x. Also, ASP.Net 1.x lacks
in providing a feature to make the validation controls applied to a group of
controls. To answer this, Asp.net 2.0 introduced a new feature with validation
control called Validation Group where we can validate group of controls
separately.
Read my previous article about Validation
Group in ASP.Net 2.0
Grouping All Validation Messages
Sometimes, we may get requirements to group all the
validation messages at one place rather than on the side of the input controls.
For example, the requirement may be placing all the validation messages on top
or bottom of the pages. This can be achieved by the help of ValidationSummary
control in asp.net.
Drag a ValidationSummary control to the place where we
need to display the error messages and set all the Validator control’s Display
property to none. This will make the validation messages to appear wherever we
placed the ValidationSummary control.
Using Image as a Validation Message in Validation
Control
We can also set some images as a validation messages
when we use validator control. For example, we can show mandatory image as a
validation message if a field is mandatory with RequiredField validator
control.
Refer the below code that sets a image as validation
message,
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1"
InitialValue="Select a Rank" runat="server"
ControlToValidate="ddlRank"
ErrorMessage="<img
src='error_arrow_bg.gif'>">
</asp:RequiredFieldValidator>
|