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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.