When I submit the form I got an alert message. When I accept the alert it will submit the form anyway. Returning false is ignored.
Onclick can not be used. I try with var x = document.forms["form"]["fname"].value;
and still same.
<form id="f" method="post" name="form" onsubmit="return validateForm();" action="#">
<input type="text" name="fname" id="test" />
<input type="submit" value="submit"/>
</form>
<script type="text/javascript">
function validateForm() {
var x = document.getElementById('test').value;
if (x == null || x == 0 || x == "0") {
alert("Stop");
return false;
}
}
</script>
When I submit the form I got an alert message. When I accept the alert it will submit the form anyway. Returning false is ignored.
Onclick can not be used. I try with var x = document.forms["form"]["fname"].value;
and still same.
<form id="f" method="post" name="form" onsubmit="return validateForm();" action="#">
<input type="text" name="fname" id="test" />
<input type="submit" value="submit"/>
</form>
<script type="text/javascript">
function validateForm() {
var x = document.getElementById('test').value;
if (x == null || x == 0 || x == "0") {
alert("Stop");
return false;
}
}
</script>
Share
Improve this question
asked Aug 28, 2013 at 5:24
Nejc GalofNejc Galof
2,6143 gold badges36 silver badges71 bronze badges
11
- 3 it is working fine, check for any error in your browser console try jsfiddle.net/arunpjohny/KJyF8/1 – Arun P Johny Commented Aug 28, 2013 at 5:29
- Working fine here: jsfiddle.net/su4jr – hjpotter92 Commented Aug 28, 2013 at 5:29
- looks similar to <stackoverflow.com/questions/3013396/…> – Prasanna Aarthi Commented Aug 28, 2013 at 5:30
- There is no error in your form. It is working fine – Saranya Sadhasivam Commented Aug 28, 2013 at 5:30
- Out of curiosity, why has this question been up-voted? – Fiddles Commented Aug 28, 2013 at 5:34
2 Answers
Reset to default 9Instead of <input type="submit" value="submit"/>
use <input type="button" value="Submit" onclick='validateForm()'/>
.
In your JS:
<script type="text/javascript">
function validateForm() {
var x = document.getElementById('test').value;
if (x == null || x == 0 || x == "0") {
alert("Stop");
}
else
document.form.submit();
}
</script>
Give this script inside of head tag and check it.
<script type="text/javascript">
function validateForm() {
var x = document.getElementById('test').value;
if (x == null || x == 0 || x == "0") {
alert("Stop");
return false;
}
}
</script>