Introduction
The Custom Validator control allows you to
create a validation control with customized validation logic. For example, you
can create a validation control that checks whether the value entered into a
text box is an even number. If none of the other validators can help you, the
Custom Validator usually can. It
doesn't come with a predefined way of working; you write the code for validating
your self. This is of course very powerful, since the possibilities are
basically endless. A common way of using the Custom Validator is when you need
to make a database lookup to see if the value is valid.
Validation controls always perform validation checking on the server. They also
have complete client-side implementation that allows DHTML supported browsers
to perform validation on the client. Client-side validation enhances the
validation process by checking user input before it is sent to the server. This
allows errors to be detected on the client before the form is submitted,
avoiding the round-trip of information necessary for server side validation.
Recall that each of ASP.NET's validation Web
controls contain a required property called
ControlToValidate.
This property specifies what form field the validation control is set to
validate. Hence, our custom validation function has the simple task before it.
- Read in the value of the form field that
the CustomValidator is set to validate.
- Perform the validation logic.
- Specify if the form field is valid or not.
parameter contains two properties that are
vitally important
Value : indicates the value of the form field the CustomValidator control is
validating.IsValid : a Boolean property that you must set to reflect if the form field's entry
is valid or not. Set IsValid
to true if the Value
is valid, false otherwise.
Example : Simple example of custom
validation is given below.
Code
<asp:CustomValidator
id="ProgrammaticID"
ControlToValidate="programmatic
ID of Server Control to Validate"
ClientValidationFunction="ClientValidateID"
OnServerValidate="ServerValidateID"
ErrorMessage="Message to
display in ValidationSummary control"
Text="Message to display in control"
ForeColor="value"
BackColor="value"
runat="server"
>
</asp:CustomValidator>