The validation controls are found in the System.Web.UI.WebControls namespace and inherit from the BaseValidator class. This class defines the basic functionality for a validation control. The next table describes its key properties:

 

Namespace: System.Web.UI.WebControls

Assembly: System.Web (in System.Web.dll)

 

Property

Description

Supported in .NET version

ControlToValidate

The property is used to identify the control that this validator will check. Each validator can verify the value in one input control. You can “stack” by attaching several validators to one input control to perform more than one type of error checking.

1.0,1.1, 2.0, 3.0, 3.5, 4.0

ErrorMessage and

ForeColor

If validation fails, the validator control can display a text message (set by the ErrorMessage property). By changing the ForeColor, you can make this message stand out in angry red lettering.

Note: In previous versions of ASP.NET, the validation controls used red lettering by default. But if you create a new application for ASP.NET 4, your validation messages will use ordinary black text, unless you set the ForeColor property or use CSS styles in your page.

1.0,1.1, 2.0, 3.0, 3.5, 4.0

Display

You can use this property to configure whether this error message will be inserted into the page dynamically when it’s needed (Dynamic) or whether an appropriate space will be reserved for the message (Static).

– Dynamic is useful when you’re placing several validators next to each other. That way, the space will expand to fit the currently active error indicators, and you won’t be left with any unseemly whitespace.

– Static is useful when the validator is in a table and you don’t want the width of the cell to collapse when no message is displayed.

– You can also choose None to hide the error message altogether.

1.0,1.1, 2.0, 3.0, 3.5, 4.0

IsValid

After validation is performed, this returns true or false depending on whether it succeeded or failed. Generally, you’ll check the state of the entire page by looking at its IsValid property instead to find out if all the validation controls succeeded.

1.0,1.1, 2.0, 3.0, 3.5, 4.0

Enabled

When set to false, automatic validation will not be performed for this control when the page is submitted.

1.0,1.1, 2.0, 3.0, 3.5, 4.0

EnableClientScript

If set to true, ASP.NET will add JavaScript and DHTML code to allow client-side validation on browsers that support it.

1.0,1.1, 2.0, 3.0, 3.5, 4.0

 

When using a validation control, the only properties you need to implement are ControlToValidate and ErrorMessage.You may need to implement the properties that are used for your specific validator which are described in the next table:

 

Validator Control

Added Members

RequiredFieldValidator

None required

RangeValidator

MaximumValue, MinimumValue, Type

CompareValidator

ControlToCompare, Operator, Type, ValueToCompare

RegularExpressionValidator

ValidationExpression

CustomValidator

ClientValidationFunction, ValidateEmptyText, ServerValidate event