Learning Horizon | For Learners

ASP.NET, SQL SERVER, JQUERY,JAVASCRIPT, WEBSPHERE

Thursday 14 March 2013

Compare Validator Control In ASP.NET

Here we go with the second tutorial of the series and today I am going to discuss and learn about CompareValidator Server Control of ASP.NET. Let's start:

2. CompareValidator Server Control:

Normally used to compare the value of one input control to the value of other input control or we can compare it with fixed value as well.

The most suitable and famous example in this regard is the password field. Suppose you have website and you want end user to sing up and for that purpose he/she needs to enter the password and confirm the password. so in that case you will surely use comparevalidator to compare both passwords that if user enter correct password in both the fields or not.

Example:-

To exercise this example

  1. Drag and drop two text boxes on your web form(in this case comparevalidator.aspx).
  2. Drag and drop comparevalidator control from toolbox on your web page.
  3. Drag and drop one button contorl and one label.

CompareValidator.aspx:-



<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Compare Validator Control</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Password<br />
        <asp:TextBox ID="txtPassword" TextMode="Password" runat="server"></asp:TextBox>
        &nbsp;
        <asp:CompareValidator ID="CompareValidator1" runat="server" ForeColor="Red" 
      ErrorMessage="Password do not match!"
            ControlToValidate="txtConfirmPassword" 
      ControlToCompare="txtPassword"></asp:CompareValidator>
        <br />
        Confirm Password<br />
        <asp:TextBox ID="txtConfirmPassword" TextMode="Password" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
        <br />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>



CompareValidator.aspx.cs:-




using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;
namespace DynamicWebLearning
{
    public partial class CompareValidator : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)         {
            if (Page.IsValid) {
                Label1.Text = "Passwords Match";
            }
        }
    }
}

After looking at the above example you can see it is similar to RequiredFieldValidator control.It also has property ContorlToValidate that associate/connect it with one of the other form elements on the page. Another property used in here is ControlToCompare and it specifies what value is compared to "txtConfirmPassword". In this case, the value is "txtPassword". Figure shown below.



Note:- If the input control is empty, the validation will succeed. Use the RequiredField Validator control to make the field required.

Hope it will be helpful. If you have any question please feel free to comment in the box below.

No comments:

Post a Comment

Please do not enter spam links.