ASP.NET 2.0 Validation Controls

November 30th, 2006 by koolb | Filed under .NET, ASP.NET, Programming, Web Development.

ASP.NET 2.0 has not introduced any new validation controls but MS has enhanced the existing validation controls a lot

Validation Groups

Now you can have multiple form areas on the same page by grouping the controls to be validated by using ValidationGroup property of controls.

This feature supports server side validations too. In Addition Page.Validate() method has been enhanced and GetValidators() method has been added. Specifically Page.Validate(ValidationGroupPropertyValue) and GetValidators(ValidationGroup) makes your life much easier

Focusing Controls

Validation Controls now have SetFocusOnError property to make the control focused in the case of an error. You can also use the Focus() method of server controls which will add client-side JavaScript to set focus to a particular control on page load

Browser compatibility

MS has used document.getElementById(id) in validation controls making client side validation possible in all the browser which supports JavaScript 1.2 or higher. This has make all the controls to be work in IE, Firefox and Opera.

Following are the Base Validation Control Properties from MSDN

ControlToValidate

The programmatic ID of the input control that the validation control will evaluate. If this is not a legitimate ID, an exception is thrown.

Display

The display behavior for the specified validation control. This property can be one of the following values:

None — The validation control is never displayed inline. Use this option when you want to show the error message only in a ValidationSummary control.

Static — The validation control displays an error message if validation fails. Space is allocated on the Web page for the error message even if the input control passes validation. The layout of the page does not change when the validation control displays its error message. Because the page layout is static, multiple validation controls for the same input control must occupy different physical locations on the page.

Dynamic — The validation control displays an error message if validation fails. Space for the error message is allocated dynamically on the page when validation fails. This allows multiple validation controls to share the same physical location on the page.

Note

Because space for the validation control is created dynamically, the physical layout of the page changes. To prevent the page layout from changing when a validation control becomes visible, the HTML element containing the validation control must be sized large enough to accommodate the maximum size of the validation control.

EnableClientScript

Indicates whether client-side validation is enabled. You can disable client-side validation on browsers that support this capability by setting the EnableClientScript property to false.

Enabled

Indicates whether the validation control is enabled. You can prevent the validation control from validating an input control by setting this property to false.

ErrorMessage

The error message to display in the ValidationSummary control if validation fails. If the Text property of the validation control is not set, this text is also displayed in the validation control when validation fails. The ErrorMessage property is commonly used to provide different messages for the validation control and the ValidationSummary control.

Note

This property does not convert special characters to HTML entities. For example, the less than character (<) is not converted to <. This allows you to imbed HTML elements, such as an <img> element, in this property’s value.

ForeColor

Specifies the color in which to display the inline message when validation fails.

IsValid

Indicates whether the input control specified by the ControlToValidate property is determined to be valid.

SetFocusOnError

Indicates whether focus is set to the control specified by the ControlToValidate property when validation fails.

Text

When set, this message is displayed in the validation control when validation fails. If this property is not set, the text specified in the ErrorMessage property is displayed in the control.

ValidationGroup

Specifies the name of the validation group to which this validation control belongs.

Share Your Thoughts