ASP.Net
validation controls validate the user input data to ensure that useless,
unauthenticated or contradictory data don’t get entered. ASP.NET validation
controls provide an easy-to-use but powerful mechanism of ensuring that data is
entered correctly on the forms. A Validation server control is used to validate
the data of an input control. If the data does not pass validation, it will display
an error message to the user.
Client-Side
and or Server-Side Validation: ASP.NET
Validation Controls provides two types of validations:
·
Client-side
validation: We can
enable or disable the Client-side validation by setting a true/false value to
the EnableClientScript property of the Validation Control. We don’t need to do
anything special for Client-Side validation except setting the property
EnableClientScript=true. By enabling Client-side validation we can reduce the
round trips.
·
Server-side
validation: If you
want to do validation with your server side code (i.e. in C#, VB.Net language
etc.), then server side validation is used.
If
you want to forcefully disable client-side validation and perform only
server-side validation for the page then set the page attribute ClientTarget
to DownLevel as:
<%@ Page ClientTarget="DownLevel" %>
Force control to both side validation as:
<%@ Page ClientTarget="UpLevel" %>
|
Server
Controls Property Related to Validation Controls
CausesValidation
and ValidationGroup are the validation control related properties associated
with some of the web server controls.
Property
|
Description
|
CausesValidation
|
Boolean value indicating whether the control
causes validation to be performed on any controls that require validation
when it receives focus. If CausesValidation property is set to true, then
validation is performed and if the CausesValidation property is set to false
then validation is not done. By default CausesValidation is set to true.
This property is available to:
·
Button, ImageButton, and LinkButton Web server
controls,
·
HtmlInputButton, HtmlInputImage, and HtmlButton
HTML server controls
·
Controls that can automatically post back to the
server such as the TextBox, CheckBox, ListControl, and BulletedList controls.
|
ValidationGroup
|
The group of controls for which the server control causes
validation when it posts back to the server.
The name of the validation group to which this validation
control belongs (Only controls having same validation group will be
validated).
If no validation group is defined then all control are validated
of page or control.
|
Page
Property or Method Related to Server-Side Validation Controls
There are
two properties and one method associated with page class related to validation
controls, they are listed below.
Property
|
Description
|
IsValid
|
Gets a value indicating whether page validation succeeded?
|
Validators
|
This property will return a collection of validation controls in
the page
|
Method
|
Description
|
Validate()
|
Validate method is the main method that will be
used for validating the validation controls from server side.
·
Validate(): Validating the All Validation
Controls with in the page
·
Validate(String): Validating Validation Controls
for a specified validation group
|
Client
Side Property or Method Related to Validation Controls
ASP.NET
validation controls provide functionality to perform validation using client
script. When client-side validation is being performed, the user cannot post
the page to the server if there are errors on the page thus reducing
round-trips. The following properties are available in client-side.
Property
|
Description
|
Page_IsValid
|
It is client side value used to identify the
page is error free from the client side itself.
|
Page_Validators
|
This property will return the collection of validation controls
in the page.
|
Page_ValidationActive
|
Indicates whether validation should take place. Set this
variable to False to turn off validation programmatically.
|
Isvalid
|
Boolean value returns the associated validation control's
validation failed or succeeds.
|
Method
|
Description
|
ValidatorValidate(val)
|
Takes a client-validator as input. Makes the
validator check its input and update its display.
|
ValidatorEnable(val, enable)
|
Enables or disables a client validator. Here
"val" is the validation control and "enable" is the
Boolean value. An example given in the section Conditional Validation.
|
ValidatorHookupControl
(control, val)
|
Takes an input HTML element and a
client-validator. Modifies or creates the element's change event so that it
updates the validator when changed. This can be useful for custom validators
that depend on multiple input values.
|
Page_ClientValidate()
|
Client-side function to validate all the Validation
Controls with in the page.
|
Page_ClientValidate(ValGroup)
|
Client-side function to validate all the
Validation Controls with in the page
with the specified ValidationGroup
|
Validation Controls: There are 6 validation controls available in the ASP.NET.
Note: You can also create your own custom
validator control, In order to create a CustomValidationControl you have to
derive from the 'BaseValidator' class and implement the 'EvaluateIsValid()'
method.
The BaseValidator Class: The
validation control classes inherit from the BaseValidator class and inherit its
properties and methods. Therefore, it would help to take a look at the
properties and the methods of this base class, which are common for all the validation
controls.
Common Properties of validation controls: Let’s look
at the common properties of all the validation controls:
Property
|
Description
|
Note
|
BackColor
|
The background color of the validator control
|
Not available for ValidationSummary
|
ControlToValidate
|
The Id of the control to validate
|
Not available for ValidationSummary
|
Display
|
The display behavior for the validation control. Legal values
are:
|
Not available for ValidationSummary
|
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
|
Not available for ValidationSummary
|
ForeColor
|
The foreground color of the control
|
|
Id
|
A unique Id for the validation control
|
|
IsValid
|
A Boolean value that indicates whether the control specified by
ControlToValidate is determined to be valid
|
Not available for ValidationSummary
|
runat
|
Specifies that the control is a server control. Must be set to
"server"
|
|
Text
|
The message to display when validation fails
|
Not available for ValidationSummary
|
You can download sample code to see all the validation control in action.
Happy Programming!!!
Comments
Post a Comment