如何在Ajax调用中调用多个验证函数?

时间:2012-01-02 15:17:58

标签: jquery ajax validation asp.net-mvc-2

我想在调用将记录插入数据库的函数之前验证我的表单。

        $(document).ready(function () {
        $("#btnSignup").click(function () {
            ///////////CAN I PUT SOMETHING HERE TO HANDLE VALIDATION?
            $.ajax({
                type: "POST",
                dataType: 'json',
                url: "/Newsletter/Signup",
                data: $('#signupForm').serialize(),
                success: function (response) {
                    if (response.success) {
                        $('#signupMessage').show(0);
                    }
                    else {
                        showValidationErrors(response.Data);
                    }
                }
            });
            return false;
        });

我是否可以在此处插入验证我的表单的内容,然后,如果验证,则继续调用其余的代码?

1 个答案:

答案 0 :(得分:0)

是的,您可以在ajax调用之前放置任何函数。

$(document).ready(function () {
    $("#btnSignup").click(function () {
        //create a function which will do the form validation and then call it here.
        if(form_valid()) // This will do the form validation and then only send the ajax call
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: "/Newsletter/Signup",
            data: $('#signupForm').serialize(),
            success: function (response) {
                if (response.success) {
                    $('#signupMessage').show(0);
                }
                else {
                    showValidationErrors(response.Data);
                }
            }
        });
        return false;
    });