Password and confirm password don't match

Sanjo Simon picture Sanjo Simon · Mar 4, 2017 · Viewed 8.3k times · Source

I have a password and confirm password field like below.enter image description here

I have used the same elements and script in 2 other different forms, but it isn't working in one of the forms.

Answer

Srikrushna picture Srikrushna · Mar 4, 2017
Password: <input id="pwd" name="pwd" type="password"><br><br>
Conform Password: <input id="cpwd" name="cpwd" type="password">
<div id="errorMsg"></div>
<script>
var password = document.getElementById('pwd');
confirm_password = document.getElementById('cpwd');
    function validatePassword() {
        if ((confirm_password.value!='')&&(password.value != confirm_password.value)) {
            document.getElementById('errorMsg').innerHTML='Passwords Don\'t Match';
        } else {
            document.getElementById('errorMsg').innerHTML='';
        }
    }
    password.onchange = validatePassword;
    confirm_password.onkeyup = validatePassword;
</script>