In this article I will explain with an example, how to perform Password and Confirm Password validation for Password TextBox using JavaScript in ASP.Net.
The values of the Password and Confirm Password TextBoxes are compared using JavaScript and if the values do not match an error message is displayed.
 
 
HTML Markup
The following HTML Markup consists of two ASP.Net TextBox and an ASP.Net Button control.
The Button has been assigned a JavaScript OnClientClick event handler.
When the Submit Button is clicked, the Validate JavaScript function gets executed.
Inside the Validate JavaScript function, the values of the Password and the Confirm Password TextBoxes are fetched and are compared.
Note: Here I am making use of ClientID inside the document.getElementById for ASP.Net TextBox selection. For more details, please refer my article Using JavaScript with Master Pages in ASP.Net.
 
If the values do not match, an error message is displayed using JavaScript Alert Message Box and False value is returned in order to stop the form submission.
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td>Password</td>
        <td><asp:TextBox ID="txtPassword" runat="server" TextMode="Password" /></td>
    </tr>
    <tr>
        <td>Confirm Password</td>
        <td><asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password" /></td>
    </tr>
    <tr>
        <td></td>
        <td><asp:Button Text="Submit" runat="server" OnClientClick="return Validate()" /></td>
    </tr>
</table>
<script type="text/javascript">
    function Validate() {
        var password = document.getElementById("<%=txtPassword.ClientID %>").value;
        var confirmPassword = document.getElementById("<%=txtConfirmPassword.ClientID %>").value;
        if (password != confirmPassword) {
            alert("Passwords do not match.");
            return false;
        }
        return true;
    }
</script>
 
 
Screenshot
Password and Confirm Password validation using JavaScript in ASP.Net
 
 
Browser Compatibility

The above code has been tested in the following browsers.

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.

 
 
Demo
 
 
Downloads