Dissecting the Validation Controls in ASP.NET 2.0By Scott Mitchell
One annoying task that most every developer has had to face in the past is form validation. Since forms are an integral part of dynamic, data-driven Web sites, it is essential that a user's query into a form fit the specified guidelines. For example, in a website like eBay where users are entering shipping and billing information, it is vital that the user enter credit card numbers, zip codes, email addresses, and other information in an acceptible format.
Prior to ASP.NET, form validation was a frustrating and tedious process. The page developer was responsible for creating the client-side and server-side logic for each and every validation check. ASP.NET version 1.0 helped end this tedium with a host of validation Web controls, making validation was as simple as adding the appropriate validation control to the web page and setting a few properties.
While validation controls in ASP.NET version 1.x greatly improved the developer's experience with adding page validation,
they still lacked some important functionality. For one, the client-side script emitted by the validation controls used
supported by Microsoft's Internet Explorer browser. This meant that browsers other than Internet Explorer could not
enjoy the client-side benefits of the validation controls. Additionally, the validation controls could not be logically
grouped, which proved irksome with pages divided up into different sections.
ASP.NET 2.0 doesn't add any new validation controls, but it does fix the validation control shortcomings in version 1.x, along with adding additional features. In this article we'll dissect the validation controls in version 2.0. Read on to learn more!
(This article does not cover the basics of ASP.NET's validation controls; for more information on that refer to Form Validation with ASP.NET - It Doesn't Get Any Easier! and User Input Validation in ASP.NET.)
One of the annoyances with ASP.NET version 1.x validation controls was that they all were checked against when a postback occurred. Granted, you could suppress all validators from firing when a particular Button control was clicked using the
CausesValidationproperty, but oftentimes a finer degree of control is needed. Oftentimes Web Forms have multiple "sections," where each section has a set of input controls and a Button that, when clicked, performs some action based on that section's controls. Ideally, each section would have section-dependent validation controls, but with ASP.NET version 1.x's limitations, developers were either forced to forgo the validation controls altogether (or forgo them in all but one section) or turn to a third-party validation solution, such as Peter Blum's Professional Validation And More (VAM). (Read a short review of mine on VAM and Peter's other product, Peter's Date Package.)
Thankfully with ASP.NET version 2.0 validation controls can be grouped. All validation controls now contain the
ValidationGroup property. The default value of this property is the empty string, which indicates that
the validation control is not part of a group. But those controls that have the same
are considered grouped. The Button, LinkButton, DropDownList, and other controls that are commonly used to submit a Web Form also contain
ValidationGroup property. When a Button with a
ValidationGroup property value is clicked
all of the validation controls on the page with the same
ValidationGroup property value are fired.
On postback, the
Page.IsValid property reflects the validity of all of the validation controls that have been
validated. By default, this is the set of validation controls whose
ValidationGroup property value equals the
ValidationGroup property value of the control that instigated the postback. However, additional validation
controls can be programmatically checked by calling the validation control's
In ASP.NET version 1.x the
Page class had a
Validate() method that could be called programmatically
which would check all validator controls in the page; furthermore, there was a
Validators property that provided
a collection of all validation controls. With version 2.0, the
has been enhanced and a new
GetValidators() method has been added. (The
Validation property remains
for backwards compatibility, and still returns all validation controls in the page.)
With 2.0 the
Validate() method can be called without passing in any parameters, just like in version 1.x. Doing
so validates against all of the validation controls in the page. The
Validate() method has an overload
that takes in a string parameter and validates against those controls whose
ValidationGroup property equals
the passed-in string to
Validate(). When a Button control causes a postback and its
property is True, internally the
Page.Validate(ValidationGroupPropertyValue) method gets called.
As in version 1.x, the
Page.Validators property returns a collection of all validation controls. The
GetValidators(ValidationGroup) method returns a collection of validation controls that belong to the
specified validation group.
The behavior of these properties and methods is illustrated in the
ValidationMethod.aspx demo available for
download at the end of this article...
Focusing On Invalid Form Fields
One of the nifty features of ASP.NET version 2.0 is that all server controls now have a
SetFocusOnErrorproperty. This property, if set to True, causes the Web control the validation control is operating on to receive focus if the validation control is invalid. This is a nice little usability touch that your users will likely appreciate.
Client-Side Validation Support in Non-Microsoft Browsers
- Build up your own validation control library from scratch,
- Use a free or third-party validation package that has done the dirty work for you. (The two options I mentioned in my article were Paul Glavich's free DomValidators controls or Peter Blum's Professional Validation And More (mentioned earlier in this article).
document.all, but also includes equivalent checks using the standard-compliant
The validation controls in ASP.NET version 2.0 have been improved from version 1.x. While there are no new validation controls, the existing controls have been enhanced to include support for validation groups and client-side support for browsers other than just Internet Explorer. While these enhancements do make ASP.NET's built-in validation controls much more useful in real-world settings, they still lack many of the more advanced features found in third-party validation packages. For most developers, however, these new additions to the validation controls will make the built-in controls sufficient for real-world use.