如何在不是提交类型但是按钮类型且没有表单的按钮单击事件上调用验证?

Chr*_*ris 9 validation asp.net-mvc jquery bootstrapvalidator

我一直试图解决这个问题,我知道只要按钮属于提交类型,就可以在表单中触发验证.但是我在视图上有一些其他选项卡,当我使用提交时,它会导致后退并返回到第一个选项卡.我不希望发生这种情况所以我将我的验证放在一个函数中,并将按钮类型更改为一个按钮并删除了表单标记,并尝试在按钮单击事件上调用验证函数并且不触发.这可能不使用表单标签吗?

我的代码就是这个......

    $(document).ready(function () {
    $("#btnUpdateModerator").click(function () {
        ValidateIt();
    });
    ValidateIt();
});
function ValidateIt() {
    var validator = $("#Users").bootstrapValidator({
        feedbackIcons: {
            valid: "glyphicon glyphicon-ok",
            invalid: "glyphicon glyphicon-remove",
            validating: "glyphicon glyphicon-refresh"
        },
        fields: {
            DealerUsername: {
                message: "Username is required",
                validators: {
                    notEmpty: {
                        message: "Please Provide Username"
                    }
                }
            },
            email: {
                message: "Email address is required",
                validators: {
                    notEmpty: {
                        message: "Please provide Email address"
                    },
                    emailAddress: {
                        message: "Email address was invalid"
                    }
                }
            },
            password: {
                validators: {
                    notEmpty: {
                        message: "Password is required"
                    },
                    stringLength: {
                        min: 6,
                        message: "Password must be at least 6 characters long"
                    },
                    different: {
                        field: "email",
                        message: "Email and Password cannot match"
                    }
                }
            },
            confirmPassword: {
                validators: {
                    notEmpty: {
                        message: "Confirm Password is required"
                    },
                    identical: {
                        field: "password",
                        message: "Password and Confirmation Password must match"
                    }
                }
            },
            department: {
                validators: {
                    notEmpty: {
                        message: "Department is Required"
                    }
                }
            }
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

Joh*_*n S 14

我认为您需要保留表单元素,但将按钮类型设置为"按钮"而不是"提交".然后,您可以使用引导验证程序的"验证"方法手动验证表单.

假设您的表单元素具有id"用户",您应该能够将代码更改为:

$(document).ready(function () {
    $("#btnUpdateModerator").click(function () {
        $('#Users').bootstrapValidator('validate');
    });
    ValidateIt();
});
Run Code Online (Sandbox Code Playgroud)

注意:ValidateIt()您编写的函数不验证表单,它在表单上设置验证器.


注意:我认为API文档位于:http://bv.doc.javake.cn/api/


关于进行ajax调用的评论后更新:

如果您想在单击按钮时进行ajax调用,但仅当表单有效时,我相信您会执行以下操作:

    $("#btnUpdateModerator").click(function () {
        $('#Users').bootstrapValidator('validate');
        if ($('#Users').bootstrapValidator('isValid')) {
            // Make the ajax call here.
        }
    });
Run Code Online (Sandbox Code Playgroud)

另一种写这个的方法是:

    $("#btnUpdateModerator").click(function () {
        var validator = $('#Users').data('bootstrapValidator');
        validator.validate();
        if (validator.isValid()) {
            // Make the ajax call here.
        }
    });
Run Code Online (Sandbox Code Playgroud)