Workshop


Quiz

1.

To have a validation Web control display a particular error message when the data is invalid, what property would you set?

2.

How does the Display property affect the display of the validation Web control's error message?

3.

Many websites allow users to create accounts. When creating an account, the user often must choose a password. When choosing a password, she must enter her desired password twice, to ensure that there were no typos. Now, imagine that you are asked to create such a web page. What validation Web control would you use to ensure that the text entered into these two text boxes was identical?

4.

True or False: A CompareValidator can be used to ensure that a user's input is a certain data type.

5.

What are regular expressions, and why is it worthwhile to have a RegularExpressionValidator validation Web control?

6.

True or False: Each user input Web control can have at most one validation Web control associated with it.

Answers

1.

The ErrorMessage property.

2.

The Display property can be set to one of three values: None, Static, and Dynamic. If Display is set to None, then the ErrorMessage property is never shown, regardless of whether the input data is valid. A value of Static allocates space on the web page for the validation Web control's error message, regardless of whether the error message is displayed. A value of Dynamic does not preallocate space for the error message. See Figures 12.22 and 12.23.

3.

You would use a CompareValidator with its ControlToValidate and ControlToCompare properties being set to the two TextBox Web controls. The CompareValidator's Operator property should be set to Equal. (Exercise 3 asks you to implement this scenario.)

4.

True.

5.

A regular expression is a string that contains characters and special symbols and specifies a general pattern. A RegularExpressionValidator is a validation Web control that validates user input using a regular expression. Such a validation control is worthwhile because it can be used to validate that a user's input is in a certain pattern. For example, you may want to ensure that the user provides his phone number as three digits, followed by a hyphen, followed by three digits, followed by a hyphen, followed by four digits. This can be easily accomplished with a RegularExpressionValidator, but could not be accomplished by any of the other validation Web controls.

6.

False. Web controls may have an arbitrary number of associated validation Web controls. For example, if you had a TextBox Web control where the user needed to enter her Social Security number, you'd want to use both a RegularExpressionValidator, to ensure that the data was entered in the proper format, and a RequiredFieldValidator, to ensure that the user supplied a value and did not leave the text box blank.

Exercises

  1. Build a simple ASP.NET page that uses a CompareValidator and two RequiredFieldValidators. Specifically, create an ASP.NET web page that prompts the user for his two favorite ice cream flavors. There should be two TextBox Web controls, one for each of the user's two favorite flavors. Add the needed validation Web controls to ensure that the user provides input for both of these TextBoxes and that the values for the two TextBoxes are different from one another.

  2. Create an ASP.NET page that prompts the user to provide her email address and the URL to her home page. Add the necessary validation Web controls to ensure that the user supplies an email address and that both the email address and home page URL are in the proper format. (Hint: The RegularExpressionValidator Regular Expression Editor contains predefined regular expressions for both Internet E-mail Addresses and Internet URLs.)

  3. Many websites allow users to create an account. The account creation process usually prompts the user, at minimum, to provide a desired username, a password, and an email address. Create an ASP.NET page that has a TextBox Web control for the user's desired username, two TextBox Web controls for the user's password, and one TextBox Web control for the user's email address.

For the user input to be valid, all TextBoxes must have a value entered. The user's email address must conform to the standard email address format, and the values entered into the two password TextBoxes must be equal. (Be sure to set the password TextBox Web controls' TextMode property to Password.)

(As we will see in Hour 20, "Managing Your Site's Users," ASP.NET includes a number of Login controls that provide the user interface for many common user-related tasks, including creating a user account. Regardless, this exercise is still worthwhile because it gives you valuable practice with working with input and validation Web controls.)




Sams Teach Yourself ASP. NET 2.0 in 24 Hours, Complete Starter Kit
Sams Teach Yourself ASP.NET 2.0 in 24 Hours, Complete Starter Kit
ISBN: 0672327384
EAN: 2147483647
Year: 2004
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net