Wednesday, April 18, 2007

Password validation using Compare Validator


Password validation using Compare Validator

The compare validator control allows you to make comparisons between two form elements as well as to compare values contained within form elements to constants that you specify. For instance, you can specify that a form element's value must be integer and greater than a specified number. You can also state that values must be strings, dates or other data types that are at your disposal.

For example, suppose that you have an application, which requires users to have passwords in order to access the site. You create one textbox asking for the users password and a second textbox, which asks the user to confirm the password. Because the textbox is in password mode, the end user cannot see what is typing- just the number of characters that she has typed. To reduce the chances of the end user mistyping her password and inputting this incorrect password into the system, you ask her to confirm the password. After the form is input into the system, you simply have to make a comparison between the two textboxes to see if they match. If they match, it is likely that the end user typed the password correctly, and you can input the password choice into the system. If the two text boxes do not match, you want the form to be valid.

The coding part of the example demonstrated this situation.

VB
<%@ page Language="VB" %>
<script runat="server">
Protected Sub Button1_Click (sender As Object, e As EventArgs)
Label1.Text= "Passwords Match"
End Sub
</script>

<html>
<head>
<title> Password validation using Compare Validator >/title>
</head>

<body>
<form runat="server">
<p> password<br />
<asp:textboxid="textbox1" runat="server" textmode="password"></asp:TextBox>
<asp:comparevalidatorid="comparevalidator1"runat="server"errormessage="passwordsdonotmatch!”controltovalidate="textbox2"controltocompare="textbox1"></asp:CompareValidator>
</p>

<p> Confirm Password<br />
<asp:textboxid="textbox2"runat="server"textmode="password"></asp:TextBox> </p>
<p>
<asp:buttonid="button1"onclick="button1_click"runat="server"text="login"></asp:Button>
</p>
<p>
<asp:label id="Label1" runat="server"> </asp:Label> </p>
</form>
</body>
</html>


C#
<%@ page Language=" C#" %>
<script runat="server">
Protected Void Button1_Click(Object sender, EventArgs e) {
Label1.Text= "Passwords Match"
}
</script>

Looking at the CompareValidator control on the form, you can see that it is similar to the RequiredFieldValidator control. The CompareValidator control has a property called ContolToValidate that associates itself with one of the form elements on the page. In this Case, you need only a single CompareVAlidatorcontrol on the page because a single comparison is made. In this example, you are making a comparison between the value of TextBox2 and that of TextBox1. Therefore, you use the ControlToCompare property. This specifies what value is compared to TextBox2. In this case, the value is TextBox1. Its as simple as that. If the two textboxes do not match after the page is posted by the end user, the value of the ErrorMessage property from the CompareValidator control is displayed in the browser.