asp.net.ph

Skip Navigation Links

CustomValidator Control

ASP.NET Syntax   ASP.NET Syntax for Validation Controls


Allows defining custom validation code.

Syntax

<asp:CustomValidator id="accessID"
   ControlToValidate = "ID of input control to validate"
   ClientValidationFunction = "ClientValidateID"
   onServerValidate = "ServerValidateID"
   ErrorMessage = "message to display in ValidationSummary control"
   Text = "message to display in control"
   ForeColor = "color value"
   BackColor = "color value"
   runat="server" />

For information on the individual members of this class, see CustomValidator in the class library.

Remarks

The CustomValidator control allows you to create a validation control with its validation handler. The control is typically used for validation purposes other than that provided by the intrinsic ASP.NET validator controls.

Validation controls always perform validation checking on the server. They also have complete client-side implementation that allows DHTML-supported browsers ( such as Internet Explorer 4.0 or later ) 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 needless round trips to the server.

To create a server-side validation function, provide a handler for the ServerValidate event that performs the validation. The string from the input control to validate can be accessed by using the Value property of the ServerValidateEventArgs object passed into the event handler as a parameter. The result of the validation is then stored in the IsValid property of the ServerValidateEventArgs object.

To create a client-side validation function, first add the server-side validation function described earlier. Next, add the client-side validation script function to the .aspx page.

If you are using VBScript, the function must be in the form:

Sub ValidationFunctionName ( source, arguments )

If you are using JScript, the function must be in the form:

Function ValidationFunctionName ( source, arguments )

Use the ClientValidationFunction property to specify the name of the client-side validation script function associated with the CustomValidator control. Since the script function gets executed on the client, the function needs to be in a language that the target browser supports, such as VBScript or JScript.

Like server-side validation, the string from the input control to validate is accessed by using the Value property of the arguments parameter. Return the result of the validation by setting the IsValid property of the arguments parameter.

CAUTION: When creating a client-side validation function, be sure to also include the server-side validation function. If you create a client-side validation function without a corresponding server-side function, it is possible for malicious code to access protected resources.

Multiple validation controls can be used with an individual input control to validate different criteria. For example, you can apply multiple validation controls on a TextBox control that allows the user to enter the quantity of items to add to a shopping cart. You can use a CustomValidator control to ensure that the value specified is less than the amount in inventory and a RequiredFieldValidator control to ensure that the user enters a value into the TextBox control.

NOTE: If the value of the control to validate is empty, no validation functions are called and validation succeeds. To prevent the user from skipping an input control, use a RequiredFieldValidator.

It is possible to use a CustomValidator control without setting the ControlToValidate property. This is commonly done when you are validating multiple input controls or validating input controls that cannot be used with validation controls, such as the CheckBox control.

In this case, the Value property of the arguments parameter passed to the ServerValidate event handler and to the client-side validation function always contains an empty string ( "" ).

However, these validation functions are still called, where appropriate, to determine validity on both the server and client. To access the value to validate, you must programmatically reference the input control you want to validate and then retrieve the value from the appropriate property.

For example, to validate a CheckBox control on the server, do not set the ControlToValidate property of the validation control and use the following code for ServerValidate event handler.

void ServerValidation ( object source, ServerValidateEventArgs args ) {
   args.IsValid = ( CheckBox1.Checked == true );
}
  C# VB

Example

The following example demonstrates how to create a CustomValidation control that validates whether the value entered in a text box is an even number on the server.

 Show me 

See Also

CustomValidator Class   Validating with a Custom Function   Data Entry Validation ( Client-Side )



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note