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" />
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" />
Share
Improve this question
edited Mar 4, 2013 at 5:00
sectus
15.5k5 gold badges58 silver badges97 bronze badges
asked Mar 4, 2013 at 4:52
Ketan patilKetan patil
951 gold badge2 silver badges7 bronze badges
4
- Please show your html code? – Roopendra Commented Mar 4, 2013 at 4:57
- where are the other controls you are calling by Id in you JS – polin Commented Mar 4, 2013 at 4:58
- Why are most of the questions being downvoted off late on SO ! – Harsha Venkataramu 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./forum/#!topic/chrome/Z3MD5Od3oQM – kiranvj Commented Mar 4, 2013 at 5:40
3 Answers
Reset to default 3For 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;
}
}
Move your validation to the form.
document.getElementById('my_form').addEventListener('submit', confirmValidation, false);
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>