0

我的 JS 验证代码有问题。当我提交表格并且出现错误时,表格不应该继续下去。但是,代码并没有停止,而是继续到下一行,虽然仍然有错误,但它显示了一条成功的消息。

而且我已经清楚地写过,例如,如果该字段为空,那么return false...

为什么代码继续到下一行,即使有return false

按一下submit,你就会明白我的意思了。

JS:

(function(window, $) {

    var Namespace = (function(Namespace) {

        Namespace = {

            // Main
            run : function() {
                this.validate.run('form');
            },              

            // Validation
            validate : {

                // error message span
                messageBox : '<span class="message" />',

                // add any field here
                fields : {
                    nameField : $('#contact-name'),
                    emailField : $('#contact-email'),
                    phoneField : $('#contact-phone')
                },

                // run validation
                run : function(formName) {

                    $(formName).on('submit', $.proxy(this.validateField, this));
                },

                validateField : function() {
                    for (var field in this.fields) {
                        if (this.fields.hasOwnProperty(field)) {
                            this.checkField(this.fields[field]);
                        }
                    }

                    $('#general-message-section').text('Form successfully sent, thank you!');
                    return false;
                },

                checkField : function(field) {

                    var messageBox = $(this.messageBox);

                    field.closest('li').find('.message').remove();

                    if (field.hasClass('required')) {
                        if (!field.val()) {
                            messageBox.text('This field is empty!');
                            field.closest('li').append(messageBox);
                            return false;
                        }
                    }

                    if (this.fields.emailField.val()) {
                        this.fields.emailField.closest('li').find('.message').remove();

                        if (!this.fields.emailField.val().match(this.regEx.email)) {
                            messageBox.text('Only email format accepted!');
                            this.fields.emailField.closest('li').append(messageBox);
                            return false;
                        }
                    }

                    if (this.fields.phoneField.val()) {
                        this.fields.phoneField.closest('li').find('.message').remove();

                        if (!this.fields.phoneField.val().match(this.regEx.numbers)) {
                            messageBox.text('Only numbers are accepted!');
                            this.fields.phoneField.closest('li').append(messageBox);
                            return false;
                        }
                    }
                },

                regEx : {
                    email : /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/,
                    numbers : /^[0-9]+$/
                }
            }
        };

        return Namespace;

    }(Namespace || {}));

    // make global
    window.Namespace = Namespace;

}(window, jQuery));

// run it...
Namespace.run();

HTML:

<p id="general-message-section"></p>
<form id="contact-form" method="post" action="#">
    <fieldset>

        <ul>
            <li>
                <label for="contact-name">Contact name *:</label>
                <input type="text" id="contact-name" tabindex="1" class="required" autofocus />
            </li>
            <li>
                <label for="contact-email">Contact email address *:</label>
                <input type="text" id="contact-email" tabindex="2" class="required" />
            </li>
            <li>
                <label for="contact-phone">Contact phone number:</label>
                <input type="text" id="contact-phone" tabindex="3" />
            </li>
            <li>
                <input type="submit" id="submit-btn" tabindex="4" value="Submit" />
            </li>
        </ul>
    </fieldset>
</form>

非常感谢

4

2 回答 2

2

我猜您在验证逻辑中缺少检查。你的代码:

validateField : function() {
    for (var field in this.fields) {
        if (this.fields.hasOwnProperty(field)) {
            this.checkField(this.fields[field]);
        }
    }

    $('#general-message-section').text('Form successfully sent, thank you!');
    return false;
},

调用checkField但不检查其结果(可能为假)。我猜你可能有类似的东西:

validateField : function() {
    for (var field in this.fields) {
        if (this.fields.hasOwnProperty(field)) {
            if(!this.checkField(this.fields[field])) {
                alert("There are errors!");
                return false;
            }
        }
    }

    $('#general-message-section').text('Form successfully sent, thank you!');
    return false;
},

当然return true;checkField如果它是正确的(最后),否则它也不起作用。

于 2013-05-06T17:00:52.070 回答
1

这将检查所有必填字段并设置valid为 false 如果任何checkField()返回 false 但不会破坏 For 循环,它将在循环后检查 valid 是否为 false 并中断:

        validateField : function() {
            var valid = true;
            for (var field in this.fields) {
                if (this.fields.hasOwnProperty(field)) {
                    if(this.checkField(this.fields[field]) === false) valid = false;
                }
            }
            if(!valid) return false;

            $('#general-message-section').text('Form successfully sent, thank you!');
        }

jsfiddle

于 2013-05-06T17:08:21.137 回答