BaseValidator Class

Serves as the abstract base class for validation controls.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public abstract class BaseValidator : Label, IValidator
public abstract class BaseValidator extends Label implements IValidator
public abstract class BaseValidator extends Label implements IValidator

The BaseValidator class provides the core implementation for all validation controls. Validation controls are used to validate user input in an associated input control. When the user enters a value that fails validation, the validation control displays an error message. Because a validation control is separated from the input control, you can position the error message anywhere on the page relative to the input control. ASP.NET provides several validation controls that perform specific types of validation. The following table describes these controls.

Validation control



Validates a value against the value entered into another input control, against a constant value, or against a proper data type by setting the CompareValidator.Operator property to ValidationCompareOperator.DataTypeCheck.


Validates a value using a user-provided custom validation routine.


Validates whether a value is within a range of values.


Validates a value using a regular expression.


Validates that a value was entered in a required field.

Validation controls always validate the associated input control on the server. Validation controls also have complete client-side implementation that allows script-enabled browsers (such as Microsoft Internet Explorer version 4.0 and 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 the round trip of information necessary for server-side validation.

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. You can use a RangeValidator control to ensure that the value entered in the TextBox control is within a set range, and a RequiredFieldValidator control to ensure that the user enters a value.

ASP.NET provides many controls that have the ability to post back to the server. When one of these controls has its CausesValidation property set to true, validation is performed when the control posts back to the server. The following controls have the ability to post back to the server:


Some of these controls post back to the server only when the AutoPostBack property is set to true.

These controls each have a ValidationGroup property that, when set, validates only the validation controls within the specified group when the control triggers a post back to the server. To group validation controls, set the ValidationGroup property of each validation control to the same value.

To associate an input control with a validation control, use the ControlToValidate property. To specify the text to display in a validation control when validation fails, use the Text property. You can also display a summary of all controls that fail validation in the page by using a ValidationSummary control. To specify the text to display in a ValidationSummary control, use the ErrorMessage property.


If you set the ErrorMessage property without setting the Text property, the value of the ErrorMessage property is also displayed in the validation control.

When using validator controls, you should always check the results of server-side validation before performing any processing. After a postback but before your event methods are called, the page calls the validator controls and aggregates their results into the Page.IsValid property. (You can also call the validator controls explicitly using the Validate method.) In your own code, you should check that the Page.IsValid property returns true before processing input. Even though script-enabled browsers might prevent a postback from occurring on the client if a validation check has failed, you should always also check Page.IsValid in server code before processing validated data.

You can also manually perform validation. To validate all validation controls on the page, use the Page.Validate method. Individual validation controls can be validated by using the Validate method of the control.


If you use the Page.IsValid property in a Page_Load method, you must first explicitly call the Page.Validate method. Because validation occurs after the Control.Load event for the page, but before the event handler for the Click or Command events, the Page.IsValid property is not updated until the Page.Validate method is called. As an alternative, you can place your code in the event handler for the Click or Command event instead of the Page_Load method.

Not all Web server controls support validation controls. The standard controls that can be validated with a validation control are:


For an input control to be validated, the System.Web.UI.ValidationPropertyAttribute attribute must be applied to the control.

When validation fails, you can set the focus on the associated input control by setting the SetFocusOnError property to true.

For a list of initial property values for an instance of BaseValidator, see the BaseValidator constructor.


The markup rendered by default for this control might not conform to accessibility standards such as the Web Content Accessibility Guidelines 1.0 (WCAG) priority 1 guidelines. For details about accessibility support for this control, see ASP.NET Controls and Accessibility.

Notes to Inheritors When you inherit from the BaseValidator class, you must override the following member: EvaluateIsValid.

The following code example demonstrates how to use multiple validation controls to verify that the user enters a valid value in a TextBox control.

<%@ Page Language="C#" %>

<script runat="server">
  void Button_Click(Object sender, EventArgs e) 
    if (Page.IsValid)
      MessageLabel.Text = "Page submitted successfully.";
      MessageLabel.Text = "There is an error on the page.";

    <form runat="server">

      <h3>Validator Example</h3>
      Enter a number from 1 to 10.
      <asp:textbox id="NumberTextBox" 

      <asp:rangevalidator id="NumberCompareValidator" 
        errormessage="Please enter a value from 1 to 10."

      <asp:requiredfieldvalidator id="TextBoxRequiredValidator" 
        errormessage="Please enter a value."


      <asp:button id="SubmitButton"
      <asp:label id="MessageLabel" 



Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0