Tuesday, June 11, 2013

How to Compare Password and confirm password using Javascript in Asp.net

Introduction: In this post I will explain how we can validate and compare the password and confirm Password in Asp.net using Javascript.
Description:
I have created a table Employee_Detail. Here ID is primary key.
ID
int
PASSWORD
varchar(50)

Add a new webform to project. Add the below mention to .aspx page.

<table border="1px solid"><tr><td>
    <b>Compare Password and Confirm Password Using Javascript in Asp.net</b>
    </td></tr><tr><td>       
    <table>
    <tr>
    <td>Password:</td><td>
        <asp:TextBox ID="txtpassword" runat="server" placeholder="Enter Password" TextMode="Password"></asp:TextBox></td>
    </tr>
    <tr><td>Confirm Password:</td><td>
        <asp:TextBox ID="txtconfirmpassword" runat="server" placeholder="ReEnter Password" TextMode="Password"></asp:TextBox></td></tr>
        <tr><td>&nbsp;</td><td>
            <asp:Button ID="btncheck" runat="server" Text="Check"
                OnClientClick="return compare();" onclick="btncheck_Click"/></td></tr>
    </table>
    </td></tr></table>


Put the below mention Javascript between Head tag (before the close of Head Tag).

<script language="javascript" type="text/javascript">
       function compare() {
        var pass = document.getElementById("txtpassword");
        var confirmpass = document.getElementById("txtconfirmpassword");
        var Password = document.getElementById('<%=txtpassword.ClientID %>').value;
        var valid = pass.value == confirmpass.value;
        if (Password == "") {
            alert("Please Enter Password");
            return false;
        }
        if (!valid) {
            alert("Password do not match");
            return false;
        }
       
        else {
           
            return true;
        }
            }
</script>
Note: Don’t forget to add connection in web.config file.
Now go to .aspx.cs page and write the below mention code:

using System.Data.SqlClient;
using System.Data;
using System.Configuration;

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        btncheck.Attributes.Add("onclick", "return compare");
    }
    protected void btncheck_Click(object sender, EventArgs e)
    {
        try
        {
            SqlCommand cmd = new SqlCommand("insert into LINQ_TABLE(PASSWORD) values (@PASSWORD)", con);
            con.Open();
            cmd.Parameters.AddWithValue("@PASSWORD", txtpassword.Text);
            cmd.ExecuteNonQuery();
            con.Close();
            txtpassword.Text = "";
            txtconfirmpassword.Text = "";
        }
        catch(Exception ex)
        {
           
        }
    }


In VB (.aspx.vb)
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("con").ToString())

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        btncheck.Attributes.Add("onclick", "return compare")
    End Sub

    Protected Sub btncheck_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btncheck.Click
        Try
            Dim cmd As New SqlCommand("insert into LINQ_TABLE(PASSWORD) values (@PASSWORD)", con)
            con.Open()
            cmd.Parameters.AddWithValue("@PASSWORD", txtpassword.Text)
            cmd.ExecuteNonQuery()
            con.Close()
            txtpassword.Text = ""
            txtconfirmpassword.Text = ""

        Catch ex As Exception
        End Try
    End Sub


Now run the project and check the result.

Is it helpful?

If yes post your comment to admire my work. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

1 comment:

  1. As with any business venture, it is of the utmost importance to do your research before deciding to go in the business of web hosting reseller packages.
    You will also need to delegate your domain name to the hosting space, details
    on this are provided by your hosting provider. You can be diligent about
    protecting your website but someone else on the same server might not be.


    Also visit my page imgur

    ReplyDelete