Wednesday, 18 December 2013

Use of CompareValidator in ASP.NET

When the values of two controls have to be compared or the value of a control has to be compared to a specified constant value, the CompareValidator control is used.
Example1:
Suppose that you want your user to be atleast 18 years old to join your website, then you have to check the age entered by the user to see whether he/she is 18 years old or not. This could be done very easily by using CompareValidator control. You just need to set the following CompareValidator properties:

ID Age_Validator
ControlToValidate Age
ErrorMessage Age should be 18 or more
Operator GreaterThanEqual
Type Integer
ValueToCompare 18

Demo:
Name
Age
Example2:
If you do not want your user to be more than 60 years old, then CompareValidator properties would be:

ID Age_Validator
ControlToValidate Age
ErrorMessage Age cannot be greater than 60
Operator LessThanEqual
Type Integer
ValueToCompare 60

Demo:
Name
Age
CompareValidator is also used to compare two passwords.CompareValidator properties to compare passwords are:

ID Password_Validator
ControlToCompare Password
ControlToValidate Confirm_Password
ErrorMessage The two passwords do not match
Operator Equal
Type String

Demo:
Password
Confirm Password

No comments:

Post a Comment