One of ASP.NET's primary goals has been to provide functionality to cover the most often used scenarios. For example, we'll see later on that authorization and authentication requirements are common among Web sites. Most sites won't let you get to the real goodies until you authenticate as a user. ASP.NET 2.0 includes some new login controls to make authorization and authentication easier.

Another scenario you often find when surfing Web sites is that most sites include a page onto which you are to enter various types of information. For example, when applying for credentials to enter a Web site, you often need to enter things such as user names and passwords. If you want to have something mailed to you, you may be asked to enter your e-mail address.

When the company sponsoring a Web site wants some information from you, they want to make sure they have accurate information. While they can't guarantee that whatever you enter is 100 percent accurate, they can at least have a fighting chance of getting accurate information by validating the fields you've entered. For example, some fields may be absolutely required, and the Web site will ensure that data is entered into them. If you're asked to enter a phone number, the site may ask for it in a certain format and then apply a regular expression to validate whatever you enter as a user. If you're asked to enter a password, the site may ask you to enter it twice to be sure you really meant what you typed.

ASP.NET includes a host of validation controls that accompany standard controls (like a TextBox) on a Web form. They work in concert with the standard controls and emit error messages (and sometimes alerts) if the user has typed in something that looks amiss.

ASP includes six validator controls:

  • RequiredFieldValidator

    Ensures that a field is filled in

  • RangeValidator

    Ensures the value represented by a control lies within a certain range

  • RegularExpressionValidator

    Validates that data within a control matches a specific regular expression

  • CompareValidator

    Ensures that the data represented by a control compares to a specific value or another control

  • CustomValidator

    Provides an opportunity to specify your own server-side and client-side validation functions

  • ValidationSummary

    Shows a summary of all the validation errors on a page

The validation controls all work the same way. First define a regular control on the page. Then place the accompanying validators wherever you want the error messages to appear on the page. The validator controls have a property named ControlToValidate. Point the validator control to the control needing validation and the rest works automatically. Of course, the validator controls have a number of properties you may use to customize the appearance of the error messages coming from the controls.

The ASP.NET validator controls work with the following server-side controls:

  • TextBox

  • ListBox

  • DropDownList

  • RadioButtonList

  • HtmlInputText

  • HtmlInputFile

  • HtmlSelect

  • HtmlTextArea

To see how they work, follow the next example, which applies validation controls to a Web form.