One Control, Multiple Validators

only for RuBoard

As previously mentioned, you most likely will need to wire up more than one valiator to one control; for example, the registration page that requires the user to enter a password twice. You most likely would want to make sure they have the fields filled out and the same password is in both TextBox controls. There are a couple benefits to using multiple validators. One benefit is that you can ensure proper data is entered by the user, and the other is that you can give the user different error messages. For instance, if you are using a RequiredFieldValidator and a CompareValidator you can set the RequiredFieldValidators error message to read "Required Field" and the the CompareValidator 's to read, "Passwords must match". Listing 9.10 contains code illustrating how to use multiple validators. This example uses code similar to the CompareValidator section of this chapter. There are two TextBox 's and one is used to enter a beginning date and the second, an ending date.

Listing 9.10 Muliple Validation Controls
 01:  <html> 02:  <body style="font-size:10"> 03:   <form runat="server"> 04:   <center> 05:   <H3>Multiple Validation Controls Example</h3> 06: 07:    <asp:ValidationSummary 08:     runat="server" 09:     DisplayMode="BulletList" 10:     ShowMessageBox="false" 11:     ShowSummary="True" 12:     HeaderText="The following fields are missing:" 13:    /> 14: 15:    <p> 16: 17:    Beginning Date: 18:    <asp:textbox 19:     runat="server" 20:     id="BeginningDate" 21:    /> 22: 23:    <asp:RequiredFieldValidator 24:     runat="server" 25:     ControlToValidate="BeginningDate" 26:     ErrorMessage="Beginning Date" 27:     Display="Dynamic" 28:    > 29:     <b>*</b> 30:    </asp:RequiredFieldValidator> 31: 32:    <asp:CompareValidator 33:     runat="server" 34:     ControlToValidate="BeginningDate" 35:     ControlToCompare="EndDate" 36:     Operator="LessThan" 37:     Type="Date" 38:     ErrorMessage="You must enter a valid date and the beginning date must be earlier graphics/ccc.gif than the ending date." 39:     Display="Dynamic" 40:    > 41:     * 42:    </asp:CompareValidator> 43: 44:    Ending Date: 45:    <asp:textbox 46:     runat="server" 47:     id="EndDate" 48:    /> 49: 50:    <asp:RequiredFieldValidator 51:     runat="server" 52:     ControlToValidate="EndDate" 53:     ErrorMessage="Ending Date" 54:     Display="Dynamic" 55:    > 56:     <b>*</b> 57:    </asp:RequiredFieldValidator> 58: 59:    <asp:CompareValidator 60:     runat="server" 61:     ControlToValidate="EndDate" 62:     ControlToCompare="BeginningDate" 63:     Operator="GreaterThan" 64:     Type="Date" 65:     ErrorMessage="You must enter a valid date and the beginning date must be earlier graphics/ccc.gif than the ending date." 66:     Display="Dynamic" 67:    > 68:     * 69:    </asp:CompareValidator> 70: 71:    <p> 72: 73:    <asp:Button 74:     runat="server" 75:     Text="Submit" 76:    /> 77: 78:   </center> 79:   </form> 80:  </body> 81: </html> 

In Listing 9.10, each TextBox control has a RequiredFieldValidator and a CompareValidator wired to it. Because of the way the TextBox controls are being validated , the page will not be valid until both TextBox controls are filled out with a valid date. The TextBox on lines 18 “21 has a date that is before that of the TextBox on lines 45 “48. You can try whatever you like to bypass this, but it will be impossible using standard means. Try running through the four steps from the CompareValidator section again and see if it will bypass the validation process again.

only for RuBoard


Programming Data-Driven Web Applications with ASP. NET
Programming Data-Driven Web Applications with ASP.NET
ISBN: 0672321068
EAN: 2147483647
Year: 2000
Pages: 170

Similar book on Amazon

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