Wednesday, September 20, 2006

Compare Validator

Compare Validator

 

In some case we may have to do a validation based on some of the underlying values/control data, for example.,

 

Now we need to check whether the Password and confrim password in the new user form are the same. In these cases compare validators can be used.

 

Lets develop a page which has two textboxes Password and confirm password and do a validation whether the password and Re-Enter password are the same.

 

Create a new Web form and place two textboxes (name them as txtPassword & txtConfirmPassword) and a button as shown in the picture below.

 

Now add a compare validator next to the confirm password textbox box, The select the validator control and open the property window Set the following properties.

 

  1. ErrorMessage = “Password doesn’t match”
  2. ControlToValidate = “txtConfirmpassword”
  3. ControlToCompare = “txtPassword”

 

CompareValidator Tag should belike this.

 

[code]

<asp:CompareValidator id=”cmpPassword” runat=”server” ErrorMessage=”Passwords doesn’t match” ControlToValidate=”txtConfirmPassword” ControlToCompare=”txtPassword”></asp:CompareValidator>

[/code]

 

Now let us see another example, to compare the text entered with some value. In most of the websites now we have some thing called us Picture verification, where in some random pictures are generated with some characters in it. A textbox will be given for the user to enter the characters displayed in the pic.

 

In this example, let us create similar one.

 

Design a page as shown below,

 

Add a text box and name it as txtString and add a compare validator, and set the following properties.

 

  1. ErrorMessage = “Entered String does not match”
  2. ControlToValidate = txtString
  3. ValueToCompare = “ZKdul”

 

[code]

<asp:CompareValidator id=”Comparevalidator1″ runat=”server” ErrorMessage=”Entered string is not correct” ControlToValidate=”txtString” ValueToCompare=”ZKdul”></asp:CompareValidator>

[/code]

 

Posted by Sadha at 12:50:26
Comments

One Response to “Compare Validator”

  1. hey,where are you from??can u email me please,thx

Leave a Reply