3}CompareValidator:-



This validation Control is used to compare the data entered by user in one Control with the data entered by the user in other Control or with fixed value.

Properties with CompareValidator:-

1)          ControlToValidate
2)          ErrorMessage
3)          Text
4)          ValidationGroup
5)          ControlToCompare
6)       Operator(options[Equal(Default)/LessThan/LesThanEqual/GreaterThanEqual/GreterThan/NotEqual/DataTypeCheck])
7)          ValueToCompare
8)          Type(options[String(Default)/Integer/Double/Date/Currency])

5)ControlToCompare:-Used to set or get the Id of the Control with which we want to compare

6) Operator:-Used to set or get the required operator value that is to be compared with.

 7) ValueToCompare:-Used to set or get a fixed value with which the user entered data we want to compare.

8) Type:-Used to set (or) get the required data type value that is to compared 
If the data entered by user in the Control to validate matches with the data entered in ControlToCompare or value to compare then CompareValidator will return true to the Webpage otherwise CompareValidator will return False to the Webpage.

Example with CompareValidator:-
Create a new webpage
Design the webpage 



  • To compare txtReTypePassword data with txtPassword data set the following properties
       Set the following properties for CompareValidator:-

ID                                  ---      CV1

ControlToCompare         ---      txtPassword

ControlToValidate           ---      txtReTypePassword

Text                               ---      Password Do not Match

Operator                        ---      Equal

Type                              ---      String

 Write the following code for submit Button:-
         
protected void btnSubmit_Click (object sender, EventArgs e)
{
        lblDisplay.Text = "Page Submitted to the Server";
}



  • Run the WebPage  & Check
 Example2  to CompareValidator with Fixed Value:- 

Validations to be performed:
 Age >35 years.

Create a new webpage, design the webpage


Set the following properties for CompareValidator1:-
 
ControlToValidate           ---      txtAge

ValueToCompare            ---      35

Operator                        ---      GreaterThanEqual

Type                              ---      Integer

Text                               ---      Enter age >=35

Write the following code in Submit Button

protected void btnSubmit_Click(object sender, EventArgs e)
{
        lblDisplay.Text = "Page Submitted";
 }

Run the WebPage  & Check


Example3  to perform DataTypeCheck using compare Validator 

validations to be performed

txtAge should Accept Digits only

Create a new webpage
Design the webpage 




Set the following properties for CompareeValidator1

ID                                  ---                CV1
 
ControlToValidate           ---                txtAge

Operator                        ---                DataTypeCheck

Type                              ---                Integer

Text                               ---                Enter Digits only
 Write the following code in Submit Button

protected void btnSubmit_Click(object sender, EventArgs e)
{
        lblDisplay.Text = "Page Submitted";
}

  •  Run the Application & Check

No comments:

Post a Comment