Section 8.3. The Compare Validator

8.3. The Compare Validator

While the ability to ensure that the user has made some sort of entry is great, you will often want to validate that the entry content is within certain guidelines. One of the most common requirements for validation is to compare the user's input to (a value constant, the value of another control, or a database value).

To see this at work, make a new copy of the RequiredValidationSummary protect and name the new web site CompareValidator . Add a new control that asks the user how many copies of the book he has purchased.

To do so, you will insert a text box, a required field validator, and a compare validator into a new row before the row for the Display Errors drop-down:

 <tr>     <td>Number purchased:</td>     <td><ASP:TextBox id="txtNumPurch" runat=server width="50px" /></td>     <td>         <asp:RequiredFieldValidator runat="server"            id="RequiredFieldValidatorNumPurch"  ControlToValidate="txtNumPurch"  SetFocusOnError=true            ErrorMessage ="You did not enter the number purchased"            Width="100%" >*         </asp:RequiredFieldValidator>         <asp:CompareValidator runat="server"            id="CompareValidatorNumPurch"  ControlToValidate="txtNumPurch"  SetFocusOnError=true            ErrorMessage ="Invalid number purchased"            Type="Integer"            Operator="GreaterThan"            ValueToCompare=0>*</asp:CompareValidator>     </td> </tr> 

Both validators are placed into the same cell in the table, and both validators validate the same control: txtNumPurch . The required field validator is needed because the compare validator will always return true for null or empty values.

The Compare validator's ValueToCompare attribute takes a constant, in this case zero. The Operator attribute determines how the comparison will be made (that is, how the input value must be related to the ValueToCompare) .

The possible values for the Operator attribute are Equal , NotEqual , GreaterThan , GreaterThanEqual , LessThan , LessThanEqual , and DataTypeCheck . In this example case, to be valid, the input value must be greater than the ValueToCompare constant. The user must order more than zero books (always my philosophy). You must use the Type attribute to tell the control what type of value it is using. The Type attribute takes one of the ValidationDataType enumerated values: Currency , Date , Double , Integer , or String . In the example, the values are compared as integers, and thus, entering (for example) a character will cause the validation to fail.

Run the application and satisfy yourself that the Compare validator does require a nonzero value to be entered.

8.3.1. Checking the Input Type

Rather than checking that the number of books purchased is greater than zero, you might want to check that it is a number (rather than a letter or date). To do this, you make a minor change to the CompareValidator .

To accomplish this change, remove the ValueToCompare attribute and change the Operator attribute from GreaterThan to DataTypeCheck . Since the Type attribute is Integer , the control will report any integer value as valid. The following code should replace the code for the CompareValidator you added in the last section:

 <asp:CompareValidator runat="server"    id="CompareValidatorNumPurch"    SetFocusOnError=true    ControlToValidate="txtNumPurch"    ErrorMessage ="Invalid number purchased"    Type="Integer"    Operator="DataTypeCheck">* </asp:CompareValidator> 

8.3.2. Comparing to Another Control

You can compare a value in one control to the value in another control rather than to a constant. A classic use of this might be to ask the user to enter his password twice and then validate that both entries are identical.

The common scenario is that you've asked the user to pick a new password. For security, when the password is entered, the text is disguised with asterisks . Because this will be the password the user will need to log in, you must validate the user entered the password as intended. The usual solution is to ask the user to reenter the password, and then you validate the same password was entered each time. The CompareValidator is perfect for this.

Insert the following code, again before the rows for the Display Errors control.

 <!-- Text fields for passwords --> <tr>     <td>Enter your password:</td>     <td>     <asp:TextBox id="txtPasswd1" runat=server        TextMode="Password"        Width="80"></asp:TextBox>     </td>     <td>     <!-- required to enter the password -->     <asp:RequiredFieldValidator runat=server        id="ReqFieldTxtPassword1"        ControlToValidate="txtPasswd1"        ErrorMessage ="Please enter your password"        Width="100%" >*</asp:RequiredFieldValidator>     </td> </tr> <!-- Second password for comparison --> <tr>     <td>Re-enter your password:</td>     <td>     <asp:TextBox id="txtPasswd2" runat=server         TextMode="Password"         Width="80"></asp:TextBox>     </td>     <td>     <!-- Second password is required -->     <asp:RequiredFieldValidator runat=server        id="ReqFieldTxtPassword2"        ControlToValidate="txtPasswd2"        SetFocusOnError=true        ErrorMessage ="Please re-enter your password"        Width="100%" >*</asp:RequiredFieldValidator>     <!-- Second password must match the first -->  <asp:CompareValidator runat=server        id="CompValPasswords"        ControlToValidate="txtPasswd2"        ErrorMessage ="Passwords do not match"        SetFocusOnError=true        Type="String"        Operator="Equal"        ControlToCompare="txtPasswd1">*</asp:CompareValidator>  </td> </tr> 

The first row contains the text box (with the attribute TextMode="Password" set) and a RequiredField validator to ensure the user doesn't leave it blank.

The second row contains a second password text box and a second RequiredField validator (again, it can't be blank), but it uses a compare validator to make sure the two controls have the same content. Notice the two properties set:

 ControlToValidate="txtPasswd2" ControlToCompare="txtPasswd1" 

This says that the CompareValidator (whose ID is CompValPasswords ) is validating the text box control whose ID is txtPasswd2 by comparing its value with the text box control whose ID is txtPasswd1. The Operator property is set to Equal and the Type property is set to String , so the two strings must match.

Both text boxes need a RequiredField validator because the CompareValidator will validate as matching a string against a null or empty string value.




Programming ASP. NET
Programming ASP.NET 3.5
ISBN: 0596529562
EAN: 2147483647
Year: 2003
Pages: 173

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