简体   繁体   中英

Form submits with passwords not matching (JavaScript)

The code I have been working on incorrectly allows the form to submit when password1 and password2 do not match. I'm stumped. Any help would be greatly appreciated. I've been using this site as a reference to learn coding. The community here is outstanding. Thanks in advance.

if ($.trim(Appery("register_firstname").val()) != "") {
    if ($.trim(Appery("register_lastname").val()) != "") {
        if (Appery("register_password1").val() == Appery("register_password2").val()) {
            if ($.trim(Appery("register_email").val()) != "") {
                if (!document.getElementsByName("register_email")[0].checkValidity || document.getElementsByName("register_email")[0].checkValidity()) {
                    if ($.trim(Appery("register_password1").val()) != "") {     
                        {signupService.execute({});
                    } 
                else {                        
                    document.getElementById("registrationpassword_error").innerHTML = "Please enter a password."; {
                    document.getElementById("register_password1").focus();
                }
        }
        }else {
            document.getElementById("registrationemail_error").innerHTML = "Email Address not valid."; 
        {
            document.getElementById("register_email").focus();
    }
        }
    } else {
        document.getElementById("registrationemail_error").innerHTML = "Please enter your email."; {
        document.getElementById("register_email").focus();
    }
        }
    } else {
        document.getElementById("registrationpassword_error").innerHTML = "Passwords do not match."; {
        document.getElementById("register_password1").focus();
    }
       }
} else {
    document.getElementById("registrationlastname_error").innerHTML = "Please enter your last name."; {
    document.getElementById("register_lastname").focus();
    }
        //alert("Please enter your last name.");
        }
    } else {
    document.getElementById("registrationfirstname_error").innerHTML = "Please enter your first name."; {
        document.getElementById("register_firstname").focus();
    }
       } 
    }

You have too many conditional checks in series, and your missing the '}' braces to close out all of the 'if' conditions your wrote.
The number of left braces and right braces should match.
I would separate your 'if' conditions so that password validation is DONE before validating other inputs. Each 'if' condition should have a 'true' / 'false' output that can be checked at the end of your code to see if all conditions are 'true'.
That is how I do my validation.
Each conditional check is separate from the others.

I am unsure about what Appery is, but if it is a normal HTML or JSP page that you are working on then, the form will submit on click of the submit button irrespective of what the above code returns.

I assume you are using a normal button for submitting the form on click on which the above code will run and signupService.execute({}); submits the form.

Did try checking with alerts that whether the control actually goes inside the last if loop?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM