Minifycode 2022-01-04 Viewed 1.1K times Jquery

This article how to validate passwords and confirm passwords with an example, TextBox using jQuery and JavaScript.
The values of the Password and Confirm Password TextBoxes are compared using jQuery and JavaScript and if the values do not match an error message is displayed.

Password and Confirm Password validation using jQuery

  The following HTML Markup consists of two HTML Password TextBoxes and a Button. The Button has been assigned a jQuery OnClick event handler.
When the Button is clicked, the values of the Password and the Confirm Password TextBoxes are fetched and are compared. 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="1" cellpadding="3" cellspacing="1">
    <tr>
        <td>
            Password:
        </td>
        <td>
            <input type="password" id="txtPassword" />
        </td>
    </tr>
    <tr>
        <td>
            Confirm Password:
        </td>
        <td>
            <input type="password" id="txtConfirmPassword" />
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <input type="button" id="btnSubmit" value="Submit" />
        </td>
    </tr>
</table>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#btnSubmit").click(function () {
            var password = $("#txtPassword").val();
            var confirmPassword = $("#txtConfirmPassword").val();
            if (password != confirmPassword) {
                alert("Passwords do not match.");
                return false;
            }
            return true;
        });
    });
</script>

 

Password and Confirm Password validation using JavaScript

The following HTML Markup consists of two HTML Password TextBoxes and a Button. The Button has been assigned a JavaScript OnClick event handler. When the 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. 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="1" cellpadding="3" cellspacing="1">
    <tr>
        <td>
            Password:
        </td>
        <td>
            <input type="password" id="txtPassword" />
        </td>
    </tr>
    <tr>
        <td>
            Confirm Password:
        </td>
        <td>
            <input type="password" id="txtConfirmPassword" />
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <input type="button" id="btnSubmit" value="Submit" onclick="return Validate()" />
        </td>
    </tr>
</table>
<script type="text/javascript">
    function Validate() {
        var password = document.getElementById("txtPassword").value;
        var confirmPassword = document.getElementById("txtConfirmPassword").value;
        if (password != confirmPassword) {
            alert("Passwords do not match.");
            return false;
        }
        return true;
    }
</script>

Password and Confirm Password validation using jQuery and JavaScript
minify code