9.4. The Compare Validator


While ensuring that the user has made an entry is very useful, you will often want to validate that the content of the entry is within certain guidelines. One of the most common requirements for validation is to compare the user's input to a constant, the value of another control, or a database value.

Add a new control to your bug-reporting dialog that will ask the user how many copies of the book he purchased. Manually type in a new row (using <tr><td> tags) above the Display Errors row, and set the prompt column to Number purchased:. The second column needs a text box (txtNumPurch), and the third column now takes two validators (just drag them both into the same column).

Figure 9-11. Summary and BulletList


Figure 9-12. Message box with List


The validator first is a RequiredFieldValidator (ID = reqFieldNumPurch and Control-ToValidate=txtNumPurch.

The RequiredFieldValidator ensures that the user does not leave the entry blank. Finally, set its ErrorMessage property to Please enter the number of books purchased, and its Text property to an asterisk.

The second validator is of type CompareValidator (which you also find in the Validation tab in the Toolbox). You'll use this validator to ensure that the user does not enter the value zero. The properties of the CompareValidator are the same as you've seen before, except as shown in Table 9-4.

Table 9-4. CompareValidator properties

Property

Value

Explanation

(ID)

CompareValidatorNumPurch

 

ControlToValidate

txtNumPurch

 

ErrorMessage

Invalid number purchased

 

Operator

GreaterThan

See explanation below.

Text

*

 

ValueToCompare

0

Value to compare the entered value with.


The Operator property is set with a drop-down list, as shown in Figure 9-13.

Figure 9-13. Compare Operator property


You can stipulate that the value entered must equal a value, or you can set an inequality, or you can even check the type of the data entered (see the next section, "Checking the Input Type").

For now, we're ensuring that a value greater than zero is chosen.

Build and run the application. When you enter 0 for the number of books purchased, your screen should look like Figure 9-14.

Figure 9-14. Comparing to zero


9.4.1. Checking the Input Type

Rather than checking that the number of books purchased is greater than zero, you might simply want to check that it is a number (rather than a letter or date). To do this, you make some minor changes to the CompareValidator, as shown in Table 9-5.

Table 9-5. CompareValidator data type check

Property

Value

(ID)

CompareValidatorNumPurch

ControlToValidate

txtNumPurch

ErrorMessage

Please enter an integer value

Operator

DataTypeCheck

Type

Integer


Once again, the Type value is a drop-down list, as shown in Figure 9-15.

Figure 9-15. CompareOperator data type check


The validator will now test the type of the entry, rather than its value.

Build and run the application. Enter nonnumeric data for the number of books purchased. The screen should resemble Figure 9-16.

Figure 9-16. Comparing against type


9.4.2. Comparing to Another Control

It is possible to 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 her password twice, and then to validate that both entries are identical. The CompareValidator is perfect for this.

Add two rows to your design, each with a text box so that the password may be entered twice, as shown in Figure 9-17.

Figure 9-17. Adding the password rows


Set the TextMode property of the text fields to Password so the entries will be hidden. Set the ID property of the first to txtPasswd1 and the ID property of the second to txtPasswd2.

All validators other than the RequiredFieldValidator consider a blank field to be valid. In fact, if one field has a value and the other field is blank, the comparison validator will return valid! To avoid this problem, add RequiredFieldValidators for both passwords.

You are now ready to validate that the entries in both text fields are identical. Add a comparison validator for the second password field (in addition to the required field validator). Set its attributes as shown in Table 9-6.

Table 9-6. CompareValidator attributes

Property

Value

(ID)

CompValPasswords

ControlToValidate

txtPasswd2

ErrorMessage

Passwords do not match

Operator

Equal

Type

String

ControlToCompare

textPasswd1


In this case, you do not have a ValueToCompare attribute, but instead you have a ControlToCompare attribute, which takes the ID of the control against which you'll compare this value.

Set the Operator attribute to Equal, which indicates that the new value must be equal to the value in the control with which you're comparing it, and set the Type of the comparison to String.

Once again, build and run the application. If you enter two different passwords, the error is reported, as shown in Figure 9-18.

If the two passwords are identical, the ComparisonValidator is satisfied, and the second password field is marked as valid.



Programming Visual Basic 2005
Programming Visual Basic 2005
ISBN: 0596009496
EAN: 2147483647
Year: 2006
Pages: 162
Authors: Jesse Liberty

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