ARTICLE

Using CompareValidator to check password in ASP.NET

Posted by Sapna Articles | ASP.NET using VB.NET January 24, 2011
In this article, we will discuss about Compare validator control used to compare two textbox value or one textbox value to another value.
 
Reader Level:

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.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

comvalid1.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.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor