admin管理员组

文章数量:1026989

I'm trying to make a simple password confirmer work in JS. Basically the user has 2 input fields, and if they match nothing happens, but if they dont match then there will be an alert window

This is also my first day doing javascript, and also my first question on Stack Overflow, apologies for any errors or mishaps. Thank You

<body>
    <form>
        Password:<br>
        <input type="password" name="password" placeholder="Password">
        <br>
        Re-enter Password:<br>
        <input type="password" name="confirmPassword" placeholder="Confirm password">
        <br>
        <input type="submit" onclick="ansValidation()" value="Sign Up">
    </form>
    <br>
    <script>
        function ansValidation() {
            var nameValue = document.getElementById("name")
            var passValue = document.getElementById("password")
            var confpassValue = document.getElementById("confirmPassword")
            if(typeof nameValue != String){
                window.alert("Please re-enter your name")
            }else if(passValue != confpassValue) {
               window.alert("Passwords do not match!")
            }
        }
    </script>
</body>

I'm trying to make a simple password confirmer work in JS. Basically the user has 2 input fields, and if they match nothing happens, but if they dont match then there will be an alert window

This is also my first day doing javascript, and also my first question on Stack Overflow, apologies for any errors or mishaps. Thank You

<body>
    <form>
        Password:<br>
        <input type="password" name="password" placeholder="Password">
        <br>
        Re-enter Password:<br>
        <input type="password" name="confirmPassword" placeholder="Confirm password">
        <br>
        <input type="submit" onclick="ansValidation()" value="Sign Up">
    </form>
    <br>
    <script>
        function ansValidation() {
            var nameValue = document.getElementById("name")
            var passValue = document.getElementById("password")
            var confpassValue = document.getElementById("confirmPassword")
            if(typeof nameValue != String){
                window.alert("Please re-enter your name")
            }else if(passValue != confpassValue) {
               window.alert("Passwords do not match!")
            }
        }
    </script>
</body>

Share Improve this question asked Nov 11, 2019 at 15:22 codingcodfishcodingcodfish 31 silver badge2 bronze badges 2
  • 4 You have to get the "value" properties of the <input> elements – Pointy Commented Nov 11, 2019 at 15:23
  • 1 console.log(passValue, confpassValue) I doubt two different input elements will ever be equal. – epascarello Commented Nov 11, 2019 at 15:25
Add a ment  | 

1 Answer 1

Reset to default 4

The document.getElementById function returns a node, not the value from the input. You need to call the value property on these nodes to access their values.

function ansValidation(ev) {
    ev.preventDefault
    // there is no input named name
    //var nameValue = document.getElementById("name").value
    var nameValue = "test";
    var passValue = document.getElementById("password").value
    var confpassValue = document.getElementById("confirmPassword").value
    // the typeof operator returns a string.
    if(typeof nameValue !== "string"){
        window.alert("Please re-enter your name")
    // we use strict validation ( !== ) because it's a good practice.
    }else if(passValue !== confpassValue) {
       window.alert("Passwords do not match!")
    }
}
<form>
    Password:<br>
    <input type="password" id="password" placeholder="Password">
    <br>
    Re-enter Password:<br>
    <input type="password" id="confirmPassword" placeholder="Confirm password">
    <br>
    <input type="button" href="#" onclick="ansValidation(event)" value="Sign Up">
</form>
 
I have also edited your code to make it work in the snippet. Here are some of the problem it had.

  • You need to pass an id="" attribute to your inputs if you want to use the getElementById function
  • the input name did not exists
  • the operator typeof returns a string, you need to pare that to a string.
  • When paring things in Javascript, it is always a good practice to strictly pare things (===).
  • I've added the event to the function and the ev.preventDefault to make sure the validation is done correctly before sending the form.

I'm trying to make a simple password confirmer work in JS. Basically the user has 2 input fields, and if they match nothing happens, but if they dont match then there will be an alert window

This is also my first day doing javascript, and also my first question on Stack Overflow, apologies for any errors or mishaps. Thank You

<body>
    <form>
        Password:<br>
        <input type="password" name="password" placeholder="Password">
        <br>
        Re-enter Password:<br>
        <input type="password" name="confirmPassword" placeholder="Confirm password">
        <br>
        <input type="submit" onclick="ansValidation()" value="Sign Up">
    </form>
    <br>
    <script>
        function ansValidation() {
            var nameValue = document.getElementById("name")
            var passValue = document.getElementById("password")
            var confpassValue = document.getElementById("confirmPassword")
            if(typeof nameValue != String){
                window.alert("Please re-enter your name")
            }else if(passValue != confpassValue) {
               window.alert("Passwords do not match!")
            }
        }
    </script>
</body>

I'm trying to make a simple password confirmer work in JS. Basically the user has 2 input fields, and if they match nothing happens, but if they dont match then there will be an alert window

This is also my first day doing javascript, and also my first question on Stack Overflow, apologies for any errors or mishaps. Thank You

<body>
    <form>
        Password:<br>
        <input type="password" name="password" placeholder="Password">
        <br>
        Re-enter Password:<br>
        <input type="password" name="confirmPassword" placeholder="Confirm password">
        <br>
        <input type="submit" onclick="ansValidation()" value="Sign Up">
    </form>
    <br>
    <script>
        function ansValidation() {
            var nameValue = document.getElementById("name")
            var passValue = document.getElementById("password")
            var confpassValue = document.getElementById("confirmPassword")
            if(typeof nameValue != String){
                window.alert("Please re-enter your name")
            }else if(passValue != confpassValue) {
               window.alert("Passwords do not match!")
            }
        }
    </script>
</body>

Share Improve this question asked Nov 11, 2019 at 15:22 codingcodfishcodingcodfish 31 silver badge2 bronze badges 2
  • 4 You have to get the "value" properties of the <input> elements – Pointy Commented Nov 11, 2019 at 15:23
  • 1 console.log(passValue, confpassValue) I doubt two different input elements will ever be equal. – epascarello Commented Nov 11, 2019 at 15:25
Add a ment  | 

1 Answer 1

Reset to default 4

The document.getElementById function returns a node, not the value from the input. You need to call the value property on these nodes to access their values.

function ansValidation(ev) {
    ev.preventDefault
    // there is no input named name
    //var nameValue = document.getElementById("name").value
    var nameValue = "test";
    var passValue = document.getElementById("password").value
    var confpassValue = document.getElementById("confirmPassword").value
    // the typeof operator returns a string.
    if(typeof nameValue !== "string"){
        window.alert("Please re-enter your name")
    // we use strict validation ( !== ) because it's a good practice.
    }else if(passValue !== confpassValue) {
       window.alert("Passwords do not match!")
    }
}
<form>
    Password:<br>
    <input type="password" id="password" placeholder="Password">
    <br>
    Re-enter Password:<br>
    <input type="password" id="confirmPassword" placeholder="Confirm password">
    <br>
    <input type="button" href="#" onclick="ansValidation(event)" value="Sign Up">
</form>
 
I have also edited your code to make it work in the snippet. Here are some of the problem it had.

  • You need to pass an id="" attribute to your inputs if you want to use the getElementById function
  • the input name did not exists
  • the operator typeof returns a string, you need to pare that to a string.
  • When paring things in Javascript, it is always a good practice to strictly pare things (===).
  • I've added the event to the function and the ev.preventDefault to make sure the validation is done correctly before sending the form.

本文标签: How to confirm two passwords are equal in JavaScriptStack Overflow