Bringing It All Together

I l @ ve RuBoard

By using the validation controls you learned about in this chapter, it is possible to create very sophisticated forms with a minimum of code. To demonstrate this capability, we will combine all the examples we have looked at so far in this chapter into one form, and combine this with a ValidationSummary control to provide a summary of all errors found on the form. This example also includes one new validation for ensuring that a user 's password and confirmed password match.

In the interests of conserving space, we won't display the full source code for this example here. Figure 8.5 shows what the end result looks like with a few errors. You can see the live version of this example with full VB and C# source code at http://aspauthors.com/aspnetbyexample/ch08/. We will just look at the new functionality of this example in the text.

Figure 8.5. Culmination.aspx with some invalid input.

As you can see, we have added two fields to our form that weren't covered elsewhere in the chapter. These are the Password and ConfirmPassword fields. Normally the form elements for the input of these fields would be set to display asterisks instead of showing the values, but for the purposes of demonstration we are using standard cleartext text boxes. The password must be at least eight characters long, it is required, and the Confirm Password field must exactly match the password field in order for the form to be valid. The code for this is displayed in Listing 8.8.

Listing 8.8 Password validation (culmination.aspx).
 <tr>     <td>         Password     </td>     <td>         <asp:textbox runat="Server" columns="30" id="password"></asp:textbox>     </td>     <td>         <asp:RequiredFieldValidator Runat="server" Display="Dynamic"     ControlToValidate="password" ErrorMessage="A password is required."     ID="password_required">         *         </asp:RequiredFieldValidator>         <asp:RegularExpressionValidator runat="server" Display="Dynamic"     ControlToValidate="password"     ErrorMessage="Password must be at least 8 characters long."     ValidationExpression=".{ 8} .*" id="RegularExpressionValidator1">         *         </asp:RegularExpressionValidator>     </td> </tr> <tr>     <td>         Confirm Password     </td>     <td>         <asp:textbox runat="Server" columns="30"     id="password_confirm"></asp:textbox>     </td>     <td>         <asp:RequiredFieldValidator Runat="server" Display="Dynamic"     ControlToValidate="password_confirm"     ErrorMessage="A confirmation password is required." ID="password_confirm_required">         *         </asp:RequiredFieldValidator>         <asp:CompareValidator Runat="server" Display="Dynamic"     ControlToValidate="password_confirm" ControlToCompare="password" Type="String"     ErrorMessage="Confirmation password does not match password."     ID="password_confirm_compare">         *         </asp:CompareValidator>     </td> </tr> 

This excerpt includes a total of four validators. Two of these are RequiredField validators, and do not need any explanation. The RegularExpressionValidator used for the password text box uses a new regular expression syntax, the use of the period "( . )" and the asterisk "( * )" The period character in a regular expression matches any character. The " { 8} " following the first period means that we want to see exactly eight instances of any character. The asterisk " * " operator matches the previous expression zero or more times. So, after we see eight characters, we want to see zero or more characters. Basically all this is doing is ensuring that the length of the password is at least eight characters long. This is a fairly trivial use for a regular expression, but by using a regular expression validator here, I have made it easy to enhance the restrictions placed on allowable passwords simply by editing the ValidationExpression . The last validator used here is the CompareValidator , which simply ensures that the Confirm Password text box's value exactly matches the Password text box's value.

Again, the full source code for this example is available on the book's support Web site, at (http://aspauthors.com/aspnetbyexample/ch08/), along with complete working examples.

I l @ ve RuBoard


Asp. Net. By Example
ASP.NET by Example
ISBN: 0789725622
EAN: 2147483647
Year: 2001
Pages: 154

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