Using CompareValidator to check password in ASP.NET

In this article, we will discuss about Compare validator control used to compare two textbox value or one textbox value to another value.
  • 6373

 

You learned about Validation controls in my previous article Validation Controls in ASP.NET, which are defined as a set of controls that offers a great deal of flexibility and comfort when user wants to write data entry pages in web application.

ASP.NET Compare Validator

Application should always ensure that valid data is recorded. To ensure valid data is collected, we apply set of validations to data we collect. Here we will discuss about Compare validator control used to compare two textbox value or one textbox value to another value.


Compare validator will accept ControlToValidate
property that should be set to your confirm password control, ControlToCompare property that should be set to your password control. Datatype property is also there to set the comparison datatype, and you can set it to true.


Designer View

comvalid 1.gif

 
Here we drop three textboxes to use the Comparison validator control which makes comparisons between two form elements. The uppermost is for username and rest of two will be used to compare password. You have enter your password in the second textbox and then confirm it again in the next textbox and now click on submit button to compare both passwords.

Default.aspx

<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="Login" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
  <title>Compare Validator</title>
  <style type="text/css">
     .style1
     {
        background-color: #E2E2E2;
     }
     .style2
     {
        background-color: #FFFFFF;
     }
  </style>
</head>
<
body style="width: 513px">
 <form id="form1" runat="server">
   <div style="font-family: Verdana; font-size: small; height: 202px;">
      <span class="style1">Enter Username:</span><b>&nbsp; </b>
      <asp:TextBox id="usernameTextBox" runat="server" Font-Bold="True"
           style="font-weight: 700" />
      <asp:RequiredFieldValidator id="usernameReq"
           runat="server"
           ControlToValidate="usernameTextBox"
           ErrorMessage="Username is required!"
           SetFocusOnError="True" ForeColor="Red" />
      <br />
      <br />
      <span class="style1">Enter Password:</span>&nbsp;
      <asp:TextBox id="passwordTextBox" runat="server"
           TextMode="Password" />
      <asp:RequiredFieldValidator id="passwordReq"
           runat="server"
           ControlToValidate="passwordTextBox"
           ErrorMessage="Password is required!"
           SetFocusOnError="True" Display="Dynamic" ForeColor="Red" />
      <br />
      <br />
      <span class="style1">Confirm Password:</span><span class="style2">&nbsp;</span>
      <asp:TextBox id="confirmPasswordTextBox" runat="server"
           TextMode="Password" />
      <asp:RequiredFieldValidator id="confirmPasswordReq"
           runat="server"
           ControlToValidate="confirmPasswordTextBox"
           ErrorMessage="Password confirmation is required!"
           SetFocusOnError="True"
           Display="Dynamic" ForeColor="Red" />
      <br />
      <br />
      <asp:Button id="submitButton"
           runat="server"
           Text="Submit"
           OnClick="submitButton_Click" Font-Names="Verdana" Font-Size="Small"
           style="font-weight: 700" Width="118px" /> 
      <br />
      <b>
      <br />
      <asp:CompareValidator id="comparePasswords"
           runat="server"
           ControlToCompare="passwordTextBox"
           ControlToValidate="confirmPasswordTextBox"
           ErrorMessage="Your passwords do not match up!"
           Display="Dynamic" Font-Names="Verdana" ForeColor="#666666" />
      </b>
   </div>
 </form>
</body>
</
html>

Defalut.aspx.vb


Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Partial Public Class Login
  Inherits System.Web.UI.Page
  Protected Sub submitButton_Click(ByVal sender As Object, ByVal e As EventArgs)
     If Page.IsValid Then
        submitButton.Text = "Valid!"
     Else
        submitButton.Text = "Invalid!"
     End If
  End Sub
End Class

Output

comvalid2.gif

You can see that Enter password textbox value is matched against Confirm Password textbox values to make sure that they match. In the above example they don't match in their values, our password is internet but we enter internat in the confirm field. So, that an error message will appear. If the passwords will match you can continue further.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.