-1

I'm working on a system that will allow users to change their password. Simple enough, but it isn't working as intended. Now granted, I just started learning JavaScript, so it could be a simple mistake on my end. It seems to ignore the JavaScript and submits anyways. If I remove my code and just put return false; in the function's block, it doesn't submit. If I put my code back in, it seems to ignore what I tell it to return and submits.

Right now I'm just putting the form together, then I'll handle the database/php side of it after the basics are complete. I want them to input their old password, new pass, and confirm new. Upon submit, I want JavaScript to validate each box to make sure it isn't empty/null, and then compare the New Pass / Confirm Pass to make sure they match before submitting. I know I can utilize the "required" property of HTML for the input boxes for some of this, and I could do ALL of this with PHP easily, but since I'm learning JavaScript, I wanted to practice this approach.

Thanks in advance!

HTML

<form name="frmChangePass" action="UserSettings.php" method="post" onsubmit="return validatePass()">
        <span>Change Password</span><br>

        <input type="password" name="oldPassword" id="oldPassword">
        <input type="password" name="newPassword" id="newPassword">
        <input type="password" name="confirmPassword" id="confirmPassword">
        <div style="display: none" id="passMismatch"></div>

        <input type="submit" name="submitPass" value="Submit" id="submitPass">

    </form>

JavaScript

<script>
    function validatePass() {
        var nP = document.getElementById("newPassword").Value;
        var cP = document.getElementById("confirmPassword").Value;
        var oP = document.getElementById("oldPassword").Value;
        var allowSubmit = false;

        if (oP == null || oP == "") {
            document.getElementById("passMismatch").innerHTML = "Please input your current password!";
            document.getElementById("passMismatch").style.display = block;
            allowSubmit = false;
        } else if (nP == null || nP == "") {
            document.getElementById("passMismatch").innerHTML = "Please input a new password!";
            document.getElementById("passMismatch").style.display = block;
            allowSubmit = false;
        } else if (cP == null || cP == "") {
            document.getElementById("passMismatch").innerHTML = "Please confirm new password!";
            document.getElementById("passMismatch").style.display = block;
            allowSubmit = false;
        } else if (cP != nP) {
            document.getElementById("passMismatch").innerHTML = "New password mismatch! Try again.";
            document.getElementById("passMismatch").style.display = block;
            allowSubmit = false;
        }
        else {
            document.getElementById("passMismatch").style.display = none;
            allowSubmit = true;
        }

        return allowSubmit;
    }
</script>
3
  • 1
    Have you tried console.log(allowSubmit) to verify it is indeed false before returning the value? Commented Apr 9, 2015 at 16:04
  • 1
    i think use value not Value Commented Apr 9, 2015 at 16:07
  • Thanks for the replies guys. I was stuck in a meeting, so I didn't get to see them sooner. I'll keep the console.log command in mind when I run into similar issues down the road. And Satish, you were right on the money with the value thing as well. Commented Apr 9, 2015 at 17:44

2 Answers 2

1

A couple things I noticed

  1. Use value instead of Value
  2. Place quotes around block and none

<script>
    function validatePass() {
        var nP = document.getElementById("newPassword").value;
        var cP = document.getElementById("confirmPassword").value;
        var oP = document.getElementById("oldPassword").value;
        var allowSubmit = false;

        if (oP == null || oP == "") {
            document.getElementById("passMismatch").innerHTML = "Please input your current password!";
            document.getElementById("passMismatch").style.display = "block";
            allowSubmit = false;
        } else if (nP == null || nP == "") {
            document.getElementById("passMismatch").innerHTML = "Please input a new password!";
            document.getElementById("passMismatch").style.display = "block";
            allowSubmit = false;
        } else if (cP == null || cP == "") {
            document.getElementById("passMismatch").innerHTML = "Please confirm new password!";
            document.getElementById("passMismatch").style.display = "block";
            allowSubmit = false;
        } else if (cP != nP) {
            document.getElementById("passMismatch").innerHTML = "New password mismatch! Try again.";
            document.getElementById("passMismatch").style.display = "block";
            allowSubmit = false;
        }
        else {
            document.getElementById("passMismatch").style.display = "none";
            allowSubmit = true;
        }

        return allowSubmit;
    }
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks bro! It was certainly a combination between those two things, and now it's working like a champ. Will note this down for future reference. Thanks again for taking time to help.
1

You could pass your form into javascript onsubmit callback using this keyword. It gives you possibility to submit your form from code.

Try this:

<form name="frmChangePass" action="UserSettings.php" method="post" onsubmit="validatePass(this); return false;">
    <span>Change Password</span><br>
    <input type="password" name="oldPassword" id="oldPassword">
    <input type="password" name="newPassword" id="newPassword">
    <input type="password" name="confirmPassword" id="confirmPassword">
    <div style="display: none" id="passMismatch"></div>
    <input type="submit" name="submitPass" value="Submit" id="submitPass">
</form>

JS:

<script>
    function validatePass(f) {
        var nP = document.getElementById("newPassword").value;
        var cP = document.getElementById("confirmPassword").value;
        var oP = document.getElementById("oldPassword").value;
        var allowSubmit = false;

        if (oP == null || oP == "") {
            document.getElementById("passMismatch").innerHTML = "Please input your current password!";
            document.getElementById("passMismatch").style.display = "block";
            allowSubmit = false;
        } else if (nP == null || nP == "") {
            document.getElementById("passMismatch").innerHTML = "Please input a new password!";
            document.getElementById("passMismatch").style.display = "block";
            allowSubmit = false;
        } else if (cP == null || cP == "") {
            document.getElementById("passMismatch").innerHTML = "Please confirm new password!";
            document.getElementById("passMismatch").style.display = "block";
            allowSubmit = false;
        } else if (cP != nP) {
            document.getElementById("passMismatch").innerHTML = "New password mismatch! Try again.";
            document.getElementById("passMismatch").style.display = "block";
            allowSubmit = false;
        }
        else {
            document.getElementById("passMismatch").style.display = "none";
            allowSubmit = true;
        }

        if(allowSubmit) {
            f.submit();
        }
    }
</script>

1 Comment

Thanks for answering bro. I love this approach to it, and it'll certainly be useful for some other things I'll be doing soon. Only problem with it is that, as some others mentioned, you'll have to place quotes around block and change .Value to .value You get a thumbs up from me also though for the alternate approach.

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.