jquery验证显示表单提交上的正确数据的错误消息

时间:2018-01-12 06:09:33

标签: javascript jquery forms validation

您有一个表单,我已经应用了jquery验证插件。一切顺利,直到我提交表格。表单的作用是检查用户名和电子邮件地址以及数据库中存储的内容,如果使用了用户名或电子邮件,则会显示错误消息。当表单元素失去焦点时,这就完成了。

所以我要做的是将用户名更改为存储在数据库中的用户名,并返回错误消息,这是正确的。然后我将用户名更改为唯一值,错误消息消失。

现在,当我提交表单时,用户名上显示的错误消息并非唯一。

因此,如果我将用户名更改为 ericy @ sa2 错误,我会更改为 danny @ sa ,这会删除错误,然后当我提交表单时弹出错误消息, ericy @ sa2 不是唯一的,显然表单元素包含 danny @ sa

这是我的jQuery代码

$(document).ready(function(){


    $( "#manageAdministratorForm" ).validate({
        onkeyup: false, //turn off auto validate whilst typing
        rules: {
            "name": "required",
            "menuAccess[]": "required",
            email: {
                required: true,
                email: true,
                remote: {
                    url: "index.php?action=MANAGE_ADMINISTRATORS_FORM",
                    type: "post",
                    data: {
                        email: function(){
                            return $('#email').val();
                        },
                        adminId: function() {
                           return $('#adminId').val();
                        },
                        subAction: "validateEmail"
                    }

                }
            },
            username: {
                required: true,
                remote: {
                    url: "index.php?action=MANAGE_ADMINISTRATORS_FORM",
                    type: "post",
                    data: {
                        username: function(){
                            return $('#username').val();
                        },
                        adminId: function() {
                            return $('#adminId').val();
                        },
                        subAction: "validateUsername"
                    }

                }
            },
            newPassword: {
                minlength: 8
            },
            confirmPassword: {
                equalTo: "#newPassword"
            }
        },
        messages: {
            "name": "A name must be entered for the administrator",
            "menuAccess[]": "Menu items must be assigned to an administrator",
            "username": {
                required: "A username must be entered for the administrator",
                remote: function() { return $.validator.format("Username {0} is already taken", $("#username").val())}
            },
            "email": {
                required: "An email must be entered for the administrator",
                email: "Please enter a valid email address",
                remote: function() { return $.validator.format("Email {0} is already taken", $("#email").val())}
            },
            "newPassword": {
                minlength: "Enter at least {0} characters for your password"
            },
            "confirmPassword": {
                equalTo: "Please enter the same password as above"
            }

        },
        errorContainer: $('#errorContainer'),
        errorLabelContainer: $('#errorContainer ul'),
        wrapper: 'li',
            submitHandler: function(form) {
                var $form = $(form);
                $form.submit();
            }    


    });
});

0 个答案:

没有答案