Validating Input Using Validation Controls


After the basic data-entry form has been created, a key requirement for completing the Web application is to provide data-validation logic for the associated controls. This makes the job of the application developer more focused on integrating the back-end business logic components with the front end, rather than worrying about validation of the parameters. In conjunction with the rich set of Web server controls, out-of-the-box ASP.NET also provides a list of prebuilt validation controls that are simple enough to be used easily, yet extensive enough to be applied in complex validation scenarios (see Listing 8.6).

Listing 8.6 Using Validation Controls to Validate Input
 <%@ Page Language="C#" %> <html> <head>    <script runat="server">     void submitButton_Click(object sender, EventArgs e) {        ...     }     void clearButton_Click(object sender, EventArgs e) {        ...    </script> </head> <body>     <form runat="server">         <table border="1">                 <tr>                     <td>Email Address</td>                     <td>  <asp:TextBox id="email" runat="server"/>   <asp:RegularExpressionValidator id="EmailValidator"   runat="server" ControlToValidate="email"   ValidationExpression="^([a-z]*)\.([a-z]*)@mycompany.com">   You should enter your valid email address.   </asp:RegularExpressionValidator>  </td>                 </tr>                 <tr>                     <td colspan="2">                         <p align="center">                             <asp:Button id="subscribeButton" runat="server"                                  Text="Submit"/></asp:Button>                             <asp:Button id="clearButton" runat="server"                                  Text="Clear"/></asp:Button>                         </p>                     </td>                 </tr>         </table>     </form> </body> </html> 

If you examine the preceding code snippet, ( especially the area in bold), you will realize that the functionality provided by this page is to enable a subscription to a particular piece of content through an email address for a specific company. Because the user of the application shouldn't be able to enter any other third-party email address and because email addresses of all users in the company are of type firstname .lastname@mycompany.com , the email must be validated before it is sent to the page that actually adds a subscription. Now you have a couple of options: You can send the data as is to another ASP.NET page or even the same page, validate the email address using a corresponding logic, or utilize the regular expression-based validation control that specifies that email addresses should correspond to the regular expression ^([a-z]*)\.([a-z]*)@mycompany.com . In plain English, the regular expression specifies that the email address should be firstname.lastname@mycompany.com , where firstname and lastname are any combination of lowercase letters (a “z). Figure 8.8 shows an example of an address being validated, and Table 8.3 lists the validation controls.

Figure 8.8. Validating input using ASP.NET validation controls.

Table 8.3. ASP.NET Validation Controls

ASP .NET VALIDATION CONTROL

DESCRIPTION

ABBREVIATED CODE SYNTAX

CompareValidator

Compares values of either two controls or a control/constant

 <asp:CompareValidator id=".."   ControlToValidate="..."   ControlToCompare="..."   Type="..."   Text="..."   runat="server"/> 

CustomValidator

Customized validation logic

 <asp:CustomValidator id="..."   OnServerValidate="..."   ControlToValidate="..."   runat="server"/> 

RangeValidator

Validates a control between a range (upper value “lower value)

 <asp:RangeValidator id="..."   ControlToValidate="..."   MinimumValue="..."   MaximumValue="..."   runat="server"/> 

RegularExpressionValidator

Regular expression-based validator

 <asp:RegularExpressionValidator id=".."  ControlToValidate="..."  ValidationExpression="..."  runat="server"/> 

RequiredFieldValidator

Validates that a user has filled a particular control

 <asp:RequiredFieldValidator id="..."  ControlToValidate="..."  Text="..."  runat="server"/> 

ValidationSummary

Displays a list of all validation error messages

 <asp:ValidationSummary id="..."  HeaderText="..."  DisplayMode="..."  runat="server"/> 


Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

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