Name
CustomValidator
Synopsis
The CustomValidator
control allows you
to define your own validation routines. A similar task could be performed by writing
manual validation code in the click event for a submit button, but using a CustomValidator
allows you to configure
client-side validation, create an error message that will be included in the
validation summary, and provide a “vote” used for the combined System.Web.UI.Page.IsValid
property along with all other
validation controls. A CustomValidator
can also be reused easily to validate multiple controls.
To provide server-side validation, create an event handler for the ServerValidate
event.
The string from the input control and the result of the validation is stored in the
provided ServerValidateEventArgs
object.
You can also perform client-side validation, which can improve the responsiveness of
your application by reducing the need for round trips to the server. However, because
client-side validation will not be performed in some browsers and is easy to
circumvent, it should never be used in place of server validation. To use client-side
validation, set the ClientValidationFunction
to the name of
a JavaScript or VBScript function in the .aspx
code portion of
your page (not the code-behind class). This script function should be in a language
that the client browser can understand, which means that JavaScript is the best
choice if you are supporting non-Microsoft browsers. The function must be in the form function ValidationFunctionName(source ...
Get ASP.NET in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.