最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - On submit form, return false not working - Stack Overflow

programmeradmin0浏览0评论

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
 |  Show 6 more comments

2 Answers 2

Reset to default 9

Instead 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>
发布评论

评论列表(0)

  1. 暂无评论