Real Time Password Matching Checker

Quick Summary

  • A jQuery keyup handler compares the password and confirm password fields and shows a live match or mismatch message before submitting.
  • Add the txtNewPassword and txtConfirmPassword classes to your inputs, and point the message at an empty container, since .html() replaces its contents.
  • The check is only a user aid, so compare both passwords again on the server before creating or updating the account.

This jQuery snippet shows a live message while the user fills in a password and a confirm password field, so they know whether the two match before they submit the form. The check runs every time a key is released in either field.

Put the code inside your jQuery(document).ready function, so the fields exist before the event is attached. In WordPress, use jQuery(document).ready(function($) { ... }); so the $ shortcut works.

JavaScript
function checkPasswordMatch() {
    var password = $(".txtNewPassword").val();
    var confirmPassword = $(".txtConfirmPassword").val();

    if ((password !== "" && confirmPassword !== "") && (password != confirmPassword)) {

        $("#input_box_1578523623_field").html("<div class='erro-mix'>Passwords do not match!</div>");
    }

    if ((password !== "" && confirmPassword !== "") && (password == confirmPassword)) {

        $("#input_box_1578523623_field").html("<div class='erro-mix-g'>Passwords match!</div>");

    }
}

$(".txtNewPassword, .txtConfirmPassword").keyup(checkPasswordMatch);

How it works

  • checkPasswordMatch() reads the values of the fields with the classes txtNewPassword and txtConfirmPassword. Add these classes to your two password inputs, or change the selectors.
  • When both fields have a value and they differ, it prints a div with the class erro-mix and the text Passwords do not match!. When they’re equal, the class is erro-mix-g and the text is Passwords match!. Style these two classes in your CSS, for example in red and green.
  • The message goes into #input_box_1578523623_field. .html() replaces everything inside that element, so point it at an empty container, not at a wrapper that holds one of the inputs.
  • The last line runs the check on keyup in either field.

Things to keep in mind

If either field is empty, the function does nothing, so the previous message stays visible. Pasting with the mouse or browser autofill doesn’t fire keyup; listening to the input event as well covers those cases.

This check only helps the user. Always compare the two passwords again on the server before you create or update the account. For WooCommerce, adding a confirm password field to the registration form includes that server-side check.

Share on X (opens in a new tab) Share on LinkedIn (opens in a new tab) Share on Facebook (opens in a new tab)

ChatGPT (opens in a new tab) Claude (opens in a new tab) Gemini (opens in a new tab) Perplexity (opens in a new tab) Grok (opens in a new tab)

More Insights

See More