MBJH
MBJH

Reputation: 1639

Form attribute "onsubmit" not working properly

<form action="Verification" method="post" onsubmit='return (if (document.getElementById("1").value===document.getElementById("2").value))' >
        Name: <input type="text" name="name" required/><br>
        Email: <input type='email' name='email' id="1" required/><br>
        Confirm Email: <input type='email' name='confirm' id="2" required/><br>
        <input type="submit" value="Sign Up" />
    </form>

I am wondering whether I can stop the form being submitted if the two emails are not the same.

Thanks in advance

Upvotes: 0

Views: 56

Answers (2)

Gaala
Gaala

Reputation: 156

You should use jQuery validate https://jqueryvalidation.org/equalTo-method/

Upvotes: 0

Wintermute
Wintermute

Reputation: 136

You'll want to do something like a javascript that compares the two values and if they aren't the same returns false. Give the two inputs a name or grab the values by id then do something like below onsubmit:

    function validateForm() {
    var x = document.forms["myForm"]["parentsEmail"].value;
    var y = document.forms["myForm"]["parentsEmailConfirmation"].value;
    if (x != y) {
        alert("Let them know it's wrong here.");
        return false;
        }
    //else finish submission process
    }

Upvotes: 1

Related Questions