0

I have a problem with a Javascript function. It is supposed to check if two input boxes in a form have the same value, using an if statement.

HTML:

<form action="confirm_account.php" method="post" id="form" onsubmit="checkpassword()">

<p style="font-family:latine;">Password: <input type="password" name="password" id="password" style="font-family:latine;" required>
</p>
<br><br>
<p style="font-family:latine;">Re-enter Password: <input type="password" name="password-reenter" id="password-reenter" style="font-family:latine;" required> </p>
<span id="password-warning" style="color:red; font-weight:bold;"></span>

Javascript:

var password = document.getElementById("password").value;
var password2 = document.getElementById("password-reenter").value;
function checkpassword(){
   if('password' == 'password2'){
       document.getElementById("form").action = "confirm_account.php";
       document.getElementById("password-warning").innerHTML = "";
}
  else{
    document.getElementById("password-warning").innerHTML = "The passwords do not match, please re-enter the password.";
    document.getElementById("form").onsubmit = "return false";
}
};

The warning message shows up, but the onsubmit is not modified.

4
  • 3
    if('password' == 'password2') you're not checking password and password2, instead checking two strings :) Commented Mar 25, 2015 at 15:19
  • what happens if you try: document.getElementById("form").onsubmit = null; Commented Mar 25, 2015 at 15:21
  • 1
    replace document.getElementById("form").onsubmit = "return false"; with return false; Commented Mar 25, 2015 at 15:21
  • I got rid of the quotes, but the now function does nothing. Commented Mar 25, 2015 at 15:23

2 Answers 2

2

Try this, it should work:

function checkpassword() {
  var password = document.getElementById("password").value;
  var password2 = document.getElementById("password-reenter").value;

  if (password === password2) {
    document.getElementById("form").action = "confirm_account.php";
    document.getElementById("password-warning").innerHTML = "";
    document.getElementById("password-success").innerHTML = "The passwords match! Well done!";
  } else {
    document.getElementById("password-warning").innerHTML = "The passwords do not match, please re-enter the password.";
    document.getElementById("password-success").innerHTML = "";
    document.getElementById("password-reenter").focus();
    return false;
  }
}
<form action="#" method="post" id="form" onsubmit="return checkpassword()">

  <p style="font-family:latine;">Password:
    <input type="password" name="password" id="password" style="font-family:latine;" required>
  </p>

  <br>
  <p style="font-family:latine;">Re-enter Password:
    <input type="password" name="password-reenter" id="password-reenter" style="font-family:latine;" required>
  </p>

  <p>
    <span id="password-success" style="color:green;"></span>
    <span id="password-warning" style="color:red; "></span>

    <input type="submit" name="btnSave" value="Try it">
</form>

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

3 Comments

I don't need the alerts, do I?
okay, one last thing. How can I make the "reenter password" box receive focus if the passwords don't match?
You can achieve it easily with the focus() method. Check my edited answer
1
var password = document.getElementById("password").value;
var password2 = document.getElementById("password-reenter").value;

In your if condition, you're not checking password and password2 instead you are actually checking the strings.

It should be

 if(password === password2){

And onsubmit is an event and you can't assign string to it. As you're calling checkpassword from onsubmit of form. To prevent form submission just return false.

var password = document.getElementById("password").value;
var password2 = document.getElementById("password-reenter").value;
function checkpassword(){
   if(password === password2){
       document.getElementById("password-warning").innerHTML = "";
   }
  else{
    document.getElementById("password-warning").innerHTML = "The passwords do not match, please re-enter the password.";
    return false;
  }
};

7 Comments

Better if you use ===
@mohamedrias, I want the variables to be global variables.
@yak613 Okay. But if you want to prevent the form from submission, just return false. You can't assign a string as event handler
onsubmit is an attribute of the form, isn't it?
@yak613 It's an event and you can bind it to form as an attribute
|

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.