简体   繁体   English

JavaScript验证失败,但表单仍提交

[英]JavaScript validation fails but form still submits

I have the following HTML form to take in user input: 我有以下HTML表单可以接受用户输入:

    <form method = "post" action = "email.php" onsubmit = "return validateForm()" id = "form1">

        <div class = "page">
            <div class = "header">
                <p>
                    <span>CiHA Webinar Booking Form</span>
                    <img src = "lifespanLogo.png" class = "logo">
                </p>
            </div>

        <div class="splitter"></div>

            <div class = "body">
                <div id = "wbForm">

                    <p>
                        <label>
                            Webinar Date
                        </label>

                        <p>
                        <select name = "date" id = "date">
                            <option value = "choose">---</option>
                            <option value = "Wednesday 22nd January">Wednesday 22nd January 2014</option>
                            <option value = "Wednesday 29th January">Wednesday 29th January 2014</option>
                        </select>
                        </p>                
                    </p>

                    <p>
                        <label>
                            Name
                        </label>

                        <input name = "name" type = "text" id = "name">
                    </p>

                    <p>     
                        <label>
                            Organisation
                        </label>

                        <input name = "org" type = "text" id = "org">
                    </p>

                    <p>     
                        <label>
                            Email Address
                        </label>

                        <input name = "email" type = "text" id = "email">
                    </p>

                    <p id="divButtonBar">
                        <input type="submit" name="submit" value="Submit">
                    </p>

                </div>
            </div>

            <div class="footer">
                &copy;Property Tectonics 2014
            </div>
        </div>                  
    </form>

Validated by this JavaScript function which at the minute only checks to see if fields are empty or not: 通过此JavaScript函数进行验证,该JavaScript函数目前仅检查字段是否为空:

<script>

    function validateForm() {
        if (validateDate()) {
            if (validateName()) {
                if (validateOrg()) {
                    if (validateEmail()) {
                        return true;
                    }
                }
            }
        }

        else { 
            return false;
        }
    }

    function validateDate(){
        var date = document.forms["form1"]["date"].value;

        if (date.trim().length == 0) {
            element = document.getElementById("date");
            alert("Please choose a date");
            element.focus();
            return false;
        }

        else {
            return true;
        }
    }

    function validateName(){
        var name = document.forms["form1"]["name"].value;

        if (name.trim().length == 0) {
            element = document.getElementById("name");
            alert("Please enter your name");
            element.focus();
            return false;
        }

        else {
            return true;
        }
    }

    function validateOrg(){
        var org = document.forms["form1"]["org"].value;

        if (org.trim().length == 0) {
            element = document.getElementById("org");
            alert("Please enter your organisation");
            element.focus();
            return false;
        }

        else {
            return true;
        }
    }

    function validateEmail(){
        var email = document.forms["form1"]["email"].value;

        if (email.trim().length == 0) {
            element = document.getElementById("email");
            alert("Please enter your email address");
            element.focus();
            return false;
        }

        else {
            return true;
        }
    }
</script>

Yet when I purposely leave fields blank the form still submits and progresses to email.php. 但是,当我故意将字段留空时,表单仍会提交并前进至email.php。 Any help here? 这里有什么帮助吗? I just can't see the issue. 我只是看不到问题。

Thanks 谢谢

This code: 这段代码:

function validateForm() {
        if (validateDate()) {
            if (validateName()) {
                if (validateOrg()) {
                    if (validateEmail()) {
                        return true;
                    }
                }
            }
        }

        else { 
            return false;
        }
    }

will only ever return false if validateDate() is false. 如果validateDate()为false,则只会返回false。

Instead try something like: 而是尝试类似:

function validateForm() {
    if  (validateDate() && validateName() && validateOrg() && validateEmail() ) {
                    return true;
    }
    else { 
        return false;
    }
}

You don't have else clauses for many of your if statements: 您的许多if语句都没有else子句:

function validateForm() {
    if (validateDate()) {
        if (validateName()) {
            if (validateOrg()) {
                if (validateEmail()) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    else { 
        return false;
    }
}

Or you could write it more simply as: 或者,您可以将其编写为:

function validateForm() {
    if (validateDate() && validateName() && validateOrg() && validateEmail()) {
        return true;
    }
    else { 
        return false;
    }
}

Or even as: 甚至作为:

function validateForm() {
    return (validateDate() && validateName() && validateOrg() && validateEmail());
}

if you're comfortable adding jQuery to this, you could use 如果您愿意在其中添加jQuery,则可以使用

$("#form1").submit(function(e) {
    e.preventDefault();
    if(validateForm()) {
         // submit the form
    }
});

see the example fiddle with jquery support: http://jsfiddle.net/2BmY8/ 请参阅带有jquery支持的小提琴示例: http : //jsfiddle.net/2BmY8/

There is no else { return false; } 没有else { return false; } else { return false; } condition for the validateName() , validateOrg() or validateEmail() structures. else { return false; } validateName()validateOrg()validateEmail()结构的条件。 They don't explicitly return false , instead they do nothing (which is enough for the form to submit). 他们没有显式地返回false ,而是什么也不做(足以提交表单)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM