Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

23. Web App Development with ASP.NET in ... > 23.6. Validation Controls - Pg. 869

23.6 Validation Controls 869 which the user can select only one. The RadioButtonList Tasks smart-tag menu provides an Edit Items... link to open the ListItem Collection Editor so that you can create the items in the list. Perform the following steps: 1. Click to the right of the HyperLink control and press Enter to create a new para- graph, then add an Image named osImage that displays the os.png image. 2. Click to the right of the Image and press Enter to create a new paragraph, then add a RadioButtonList . Set its (ID) property to osRadioButtonList . Use the ListItem Collection Editor to add the items shown in Fig. 23.22. 3. Finally, click to the right of the RadioButtonList and press Enter to create a new paragraph, then add a Button . A Button web control represents a button that triggers an action when clicked. Set its (ID) property to registerButton and its Text property to Register . As stated earlier, clicking the Register button in this example does not do anything. You can now execute the application (Ctrl + F5) to see the Web Form in your browser. 23.6 Validation Controls This section introduces a different type of web control, called a validation control or vali- dator, which determines whether the data in another web control is in the proper format. For example, validators can determine whether a user has provided information in a re- quired field or whether a zip-code field contains exactly five digits. Validators provide a mechanism for validating user input on the client. When the page is sent to the client, the validator is converted into JavaScript that performs the validation in the client web browser. JavaScript is a scripting language that enhances the functionality of web pages and is typi- cally executed on the client. Unfortunately, some client browsers might not support script- ing or the user might disable it. For this reason, you should always perform validation on the server. ASP.NET validation controls can function on the client, on the server or both. Validating Input in a Web Form The Web Form in Fig. 23.25 prompts the user to enter a name, e-mail address and phone number. A website could use a form like this to collect contact information from visitors. After the user enters any data, but before the data is sent to the web server, validators en- sure that the user entered a value in each field and that the e-mail address and phone-num- ber values are in an acceptable format. In this example, (555) 123-4567, 555-123-4567 and 123-4567 are all considered valid phone numbers. Once the data is submitted, the web server responds by displaying a message that repeats the submitted information. A real business application would typically store the submitted data in a database or in a file on the server. We simply send the data back to the client to demonstrate that the server re- ceived the data. To execute this application: 1. Select Open Web Site... from the File menu. 2. In the Open Web Site dialog, ensure that File System is selected, then navigate to this chapter's examples, select the Validation folder and click the Open Button . 3. Select Validation.aspx in the Solution Explorer , then type Ctrl + F5 to execute the web application in your default web browser.