0

I have a registration form. It successfully checks the value of password input and confirm_password input. (X changes to ✔️ if both passwords match) The thing is, after check mark appears, it looks like it gets stuck there! when I change the password, the check mark should change to X but it doesn't! It's stuck on ✔️!

<div    id="confirm_pass2">
  <span id="password_confirmation"  class="unconfirmed_pass"></span>
</div>

  <script>
        var confirmation = document.getElementById("confirm_password");
                confirmation.onfocus = function (){
                document.getElementById("confirm_pass2").style.display = "block";
            }
            confirmation.onblur = function (){
                document.getElementById("confirm_pass2").style.display = "none";
            }
            confirmation.onkeyup = function () {
        if (document.getElementById("confirm_password").value.match(document.getElementById("password").value)){
            document.getElementById("password_confirmation").classList.remove("unconfirmed_pass");
            document.getElementById("password_confirmation").classList.add("confirmed_pass");
        }   }
        </script>



<style>
 #confirm_pass2 {
    position: absolute;
    display: none;
}
.confirmed_pass:before {
    position: absolute;
    font-size: 25px;
    right: 642px;
    bottom: 197px;
    font-weight: bold;
    color: green;
    content: "✔";
}
.unconfirmed_pass:before {
    font-family: calibri;
    position: absolute;
    right: 647px;
    bottom: 205px;
    font-size: 22px;
    font-weight: bold;
    color: red;
    content: "X";
}
</style>

UPDATE I changed "match" to == and everything seems to be working fine.

if (document.getElementById("confirm_password").value == document.getElementById("password_new").value){...
2
  • I have another checker which validates the password to meet the requirements. I've done it exactly like this and it has no problem! I don't know what's wrong with this one.. Commented Apr 17, 2020 at 22:49
  • P.S: Just found out another problem! If my password is fx and my confirm_password is asdsadasfx, they're considered a match! Commented Apr 17, 2020 at 23:09

1 Answer 1

0

How about you try adding an else condition there something like

confirmation.onkeyup = function () { 
if (document.getElementById("confirm_password").value.match(document.getElementById("password_new").value)){ 
document.getElementById("password_confirmation").classList.remove("unconfirmed_pass"); document.getElementById("password_confirmation").classList.add("confirmed_pass"); 
} else {
document.getElementById("password_confirmation").classList.remove("confirmed_pass"); document.getElementById("password_confirmation").classList.add("unconfirmed_pass");
}
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Good idea. I tried it just now. Now it changes back to X if I delete what's typed in confirm_password. But it's still not LIVE. You know what I mean? It doesn't change instantly.
Try changing onkeyup to oninput it might be faster

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.