开发者

Postback method not triggered with jQuery Validate

开发者 https://www.devze.com 2023-02-04 11:52 出处:网络
I have a strange behaviour of my jQuery Validate script. I have a simple form with a submit button (ID = _btnSave).

I have a strange behaviour of my jQuery Validate script.

I have a simple form with a submit button (ID = _btnSave). I validate three text fields : firstname, lastname and email.

[NOT WORKING] - When the form is filled in correctly, the method on my submit button is not fired : OnClick="_btnSave_Click".

[WORKING] - I write down a wrong email address, then I correct it, click on the submit button, my server-side method is called.

To summarize the situation : If I don't trigger the validation of the email, _btnSave_Click is not fired.

The page is refreshed in both cases.

<asp:Button ID="_btnSave" OnClick="_btnSave_Click" CssClass="button big default" runat="server" Text="<%$ Resources:Resource, SAVE_CHANGES %>" />

$("#form1").validate({
            rules:{
                <%=_txtEmail.UniqueID %>:{ minlength:5, required:true, email:true, 
                    remote: { 
                        url: "/Services/ValidatorService.asmx/ValidateEmail",
                        dataType:"json",
                        type:"post",
                        data: { 
                            email: function() { return $("#<%=_txtEmail.ClientID%>").val();},
                            contactUID: function() { return $("#<%=_hidContactUID.ClientID%>").val();}
                        }
                    }
                },
                <%=_txtFirstName.UniqueID %>:   { required:true},
            开发者_如何学JAVA    <%=_txtLastName.UniqueID %>:    { required:true},
            },
            invalidHandler: function(form, validator) {
                errorContainer.fadeIn(); 
            },
            errorElement: "span",
            errorClass:"errortxt",
            highlight: function(element, errorClass) {
                $(element).parent().parent().addClass("error");
            },
            unhighlight: function(element, errorClass) { 
                    $(element).parent().parent().removeClass("error");
                if (this.numberOfInvalids() == 0) { 
                    errorContainer.fadeOut(); 
                }
            },
            messages: { <%=_txtEmail.UniqueID %>: { remote : "<%=EmailUsed %>" }
            },
            errorPlacement: function(error, element) {
                    error.insertAfter(element);
            }
        });

        $("input").click(function(){_defaultButton = $("#<%=_btnSave.ClientID%>");});   
    });

Thanks for your help.

Greg


I know this question is old, but I was experiencing the same problem recently and could not find an answer online. Adding a custom submit handler in the validator options fixed the problem for me:

        $("#form1").validate({
            submitHandler: function (form) {
                __doPostBack("_btnSave", "OnClick");
            }
            // your other options
        });
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号