Compare Two Fields For Validation

You can compare two values on a form when submitting a form to the server. To do so, you can use the CompareValidator control that comes with ASP.NET. You can do this comparision based on a number of operations, including: checking the controls to see if they are equal to each other, if one is greater than or equal to another, or if one is less than another.

To use this validation control, you must first create the two controls to use as criteria for the validation. You then need to create a CompareValidator control for each comparision you would like to make. The syntax for declaring a CompareValidator control is <ASP:COMPAREVALIDATOR RUNAT="Server">.

After you declare the control, you must specify the control to validate with the controltovalidate attribute, the control to compare with the controltocompare attribute, and finally the operator with the operator attribute. After you have set these, you can then designate the validation message to be displayed with the text attribute.

For example, you can create a form that requires one of the answers to be less than or equal to another one of the answers. If the input is invalid, you would then display a validation message.

COMPARE TWO FIELDS FOR VALIDATION

COMPARE TWO FIELDS FOR VALIDATION

D Open the

GenericTemplate.aspx template from the Code Templates directory.

0 Add a message to the user.

Q Add a server form to the page.

0 Add a message to the user and TextBox control to the form.

^0 Add another message to the user and TextBox control to the form.

□ Add a Button control to the form.

L-° Add the CompareValidator control to the page.

D Open the

GenericTemplate.aspx template from the Code Templates directory.

0 Add a message to the user.

Q Add a server form to the page.

0 Add a message to the user and TextBox control to the form.

^0 Add another message to the user and TextBox control to the form.

□ Add a Button control to the form.

L-° Add the CompareValidator control to the page.

Was this article helpful?

0 0

Post a comment