0
function confirmValidation() {
    var confirmPass = document.getElementById('newpassword').value;
    var newpass = document.getElementById('newpassword2').value;
    var passLength = confirmPass.length;
    if ((confirmPass == newpass) && (confirmPass != "" && newpass != "") && (passLength > 8)) {
        alert("ok");
        return true;
    } else {
        alert("no");
        confirmPass.focus();
        return false;
    }
}

And input:

<input name="register" type="submit" class="alert_button" onclick="return confirmValidation()" value="OK" />
4
  • Please show your html code? Commented Mar 4, 2013 at 4:57
  • where are the other controls you are calling by Id in you JS Commented Mar 4, 2013 at 4:58
  • Why are most of the questions being downvoted off late on SO ! Commented Mar 4, 2013 at 4:59
  • Which browser are you using? I have heard Chrome had some issues earlier with onSubmit return. productforums.google.com/forum/#!topic/chrome/Z3MD5Od3oQM Commented Mar 4, 2013 at 5:40

3 Answers 3

3

For the return method to work, you need to specify onSubmit attribute/listener to the form tag itself. Like so:

<form method="post" onsubmit="return confirmValidation();">
    <!-- OTHER form fields -->
</form>

From w3c documentation:

The onsubmit event occurs when a form is submitted. It only applies to the FORM element.


EDIT

jsfiddle link added.

You must alter your JavaScript a little so that .focus() may fire.

function confirmValidation() {
    var confirmPass = document.getElementById('newpassword');
    var newpass = document.getElementById('newpassword2');
    var passLength = confirmPass.value.length;
    if ((confirmPass.value == newpass.value) && (confirmPass.value != "" && newpass.value != "") && (passLength > 8)) {
        alert("ok");
        return true;
    } else {
        alert("no");
        confirmPass.focus();
        return false;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

<form id="emailvalidate" name="emailvalidate" action="xyz.php" method="post" onsubmit="confirmValidation()"> <div class="login_title">Create new password</div> <dl> <dt>New Password</dt> <dd><input name="newpassword" type="password" id="newpassword" size="40"> <div class="register_alert">over 8 characters</div></dd> <dt>Confirm</dt> <dd><input name="newpassword2" type="password" id="newpassword2" size="40"></dd> <dt></dt> <dd><input name="register" type="submit" class="alert_button" value="OK"> <div class="login_alert">Password doesn't match.</div> </dd> </dl> </form>
@user2122323 Edited reply. Please check.
1

Move your validation to the form.

document.getElementById('my_form').addEventListener('submit', confirmValidation, false);

Comments

1

You should use onsubmit() event in tag. Now when you submit the form, it will call the function that is written on the onsubmit() event. Only in case of Onsubmit(), it checks the return value if it's true then it proceeds further otherwise not.

<form method="post" onsubmit="return confirmValidation();">

</form>

Comments

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.