Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

ASP.NET CustomValidator Control

ASP.NET CustomValidator Control


Validation Server Controls Validation Server Controls


Definition and Usage

The CustomValidator control allows you to write a method that handles the validation of input values.


property

property Description
BackColor The background color of the CustomValidator control.
ClientValidationFunction Specifies the name of the client-side script function to be executed. **Note:** The script must be written in a language supported by the browser, such as VBScript or JScript

When using VBScript, the function must be inside the form:

Sub FunctionName (source, arguments)

When using JScript, the function must be inside the form:

Function FunctionName (source, arguments)

ControlToValidate The id of the control to be validated.
Display The display behavior of the validation control. Valid values are:
  • None - The control is not displayed. Used only to display an error message in the ValidationSummary control.
  • Static - If validation fails, the control displays the error message. Even if the input passes validation, space for the message is reserved on the page (i.e., the space for displaying the message is pre-allocated).
  • Dynamic - If validation fails, the control displays the error message. If the input passes validation, no space for the message is reserved on the page (i.e., the space for displaying the message is added dynamically).
  • | | EnableClientScript | Boolean, specifies whether to enable client-side validation. | | Enabled | Boolean, specifies whether to enable the validation control. | | ErrorMessage | The text displayed in the ValidationSummary control when validation fails. **Note:** If the Text property is not set, the text is also displayed in the validation control. | | ForeColor | The foreground color of the control. | | id | The unique id of the control. | | IsValid | Boolean, indicates whether the control specified by ControlToValidate passed validation. | | OnServerValidate | Specifies the name of the server-side validation script function to be executed. | | runat | Specifies that this control is a server control. Must be set to "server". | | Text | The message displayed when validation fails. |

    Example

    CustomValidator
    In this example, we declare two Label controls, one TextBox control, one Button control, and one CustomValidator control in the .aspx file. The user() function checks the length of the input value. If the length is less than 8 or greater than 16, the text "A username must be between 8 and 16 characters!" is displayed in the CustomValidator control.


    Validation Server Controls Validation Server Controls