admin管理员组文章数量:1399347
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>
本文标签: return false is not working in javascript validationStack Overflow
版权声明:本文标题:return false is not working in javascript validation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744139931a2592573.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论