0

I have a registration webpage where a user inputs information like name and password. There are two inputs for password to verify they are the same password but when I submit the form, it says the passwords don't match, even when they do.

<form id="registration-info" method="POST" action="/registration" >

...

<div class="mb-3">
                <label for="password">Password</label>
                <input type="password" class="form-control" name="password" id="password" required>
                <div class="invalid-feedback">
                    Please enter a password.
                </div>
            </div>

            <div class="mb-3">
                <label for="repeat_password">Repeat Password</label>
                <input type="password" class="form-control" name="repeat_password" id="repeat_password" required>
                <script>
                    form = document.getElementById("registration-info");
                    form.onclick = function() {
                        var password = document.getElementById("password");
                        var repeat_password = document.getElementById("repeat_password");

                        if(password.value != repeat_password.value) {
                            repeat_password.setCustomValidity("Passwords Don't Match");
                        } else {
                            repeat_password.setCustomValidity('');
                        }
                    }
                </script>

            </div>
4
  • You should try to put your function on the "onsubmit" event of your form Commented Feb 18, 2018 at 23:48
  • i have tried that but then it submits the form regardless of if the passwords are not matching Commented Feb 18, 2018 at 23:50
  • if(password.value != repeat_password.value) { repeat_password.setCustomValidity("Passwords Don't Match"); return false; } else { document.repeat_password.setCustomValidity(''); } Commented Feb 18, 2018 at 23:53
  • What does the debugger say the contents of the fields are at the IF? Commented Feb 19, 2018 at 0:12

2 Answers 2

2

There are two problems with your code.

  1. You've put your validation code in an onclick handler on the <form> element. This means the script will never run at all, because the user doesn't click on the <form>, they click on the submit <button>. Instead use an onsubmit handler on the form.
  2. You aren't doing anything to prevent the form from submitting if the password values don't match. One way to do this is to return false from the onsubmit handler.

Here is a corrrected version:

form = document.getElementById("registration-info");

form.onsubmit = function() {
  var password = document.getElementById("password");
  var repeat_password = document.getElementById("repeat_password");
  if (password.value != repeat_password.value) {
    repeat_password.setCustomValidity("Passwords Don't Match");
    console.log("Passwords don't match");
    return false; // prevent the form from submitting
  } else {
    repeat_password.setCustomValidity('');
  }
}
 
// reset the customValidity when the field is modified, so corrected
// values won't trip up on past errors:
document.getElementById("repeat_password").onchange = function(e) {
  e.target.setCustomValidity('')
}
.invalid-feedback {display:none}
<form id="registration-info" method="POST" action="/registration">
  <div class="mb-3">
    <label for="password">Password</label>
    <input type="password" class="form-control" name="password" id="password" required>
    <div class="invalid-feedback">
      Please enter a password.
    </div>
  </div>

  <div class="mb-3">
    <label for="repeat_password">Repeat Password</label>
    <input type="password" class="form-control" name="repeat_password" id="repeat_password" required>
  </div>
  <input type="submit" value="Submit" id="registration-info-submit">
</form>

Another way to do this -- and to be honest if I'd been familiar with setCustomValidity before this question, this probably would have been my answer in the first place -- might be to set the customValidity message values when the field values change, instead of on form submit. (If a customValidity value is set, it will prevent the form submit from running at all.)

document.getElementById("registration-info").onchange = function() {
  var password = document.getElementById("password");
  var repeat_password = document.getElementById("repeat_password");
  if (password.value != repeat_password.value) {
    repeat_password.setCustomValidity("Passwords Don't Match");
  } else {
    repeat_password.setCustomValidity('');
  }
}
<form id="registration-info" method="POST" action="/registration">
  <div class="mb-3">
    <label for="password">Password</label>
    <input type="password" class="form-control" name="password" id="password" required>
  </div>

  <div class="mb-3">
    <label for="repeat_password">Repeat Password</label>
    <input type="password" class="form-control" name="repeat_password" id="repeat_password" required>
  </div>
  <input type="submit" value="Submit" id="registration-info-submit">
</form>

(But note that this will leave your forms unvalidated in IE9 and below, which do not support setCustomValidity; the first snippet will validate the form in all browsers.)

Sign up to request clarification or add additional context in comments.

5 Comments

this works, thanks! Also, I was using this method developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/…
Ah, my apologies! I'd not seen that method before; thought it was purpose-built code. I'll edit the answer.
And I also now see there are really only two issues here, not three: you were in fact correctly comparing the form values, just in a different place than I expected. Not my finest hour, apologies again
no worries :) actually i'm still having problems because since we added the setCustomValidity() lines back, this is what happens: if the passwords don't match, it will say they dont match but if you try to retype the passwords and they do match, it will still give the message of them not matching
Looks like any already-set CustomValidity value prevent the onsubmit handler from running at all. I added a "change" handler on that field to reset the message.
0

You did not take the values of the selected ids here. Inatead of taking values after in IF case try the following code. I hope that is the only reason.

var password = document.getElementById("password").value; 
var repeat_password = document.getElementById("repeat_password").value; 

1 Comment

I've tried your code but it still lets me submit even if passwords are wrong

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.