简体   繁体   English

jQuery验证插件。 回发后验证不起作用

[英]jQuery validation plugin. Validation not working after post back

I have set up validation on a .Net form with the JQuery plugin, everything works on page load, but after a post back the validation stops working. 我已经使用JQuery插件在.Net表单上设置了验证,所有内容都可以在页面加载时正常工作,但是在回发验证之后,该工作就停止了。

Then if I empty a requiered field and try to submit, when my .Net validators catch the problem client side, then the live validation on the fields starts working again. 然后,如果我清空一个必填字段并尝试提交,则当我的.Net验证程序发现问题客户端时,该字段上的实时验证将再次开始工作。

Here is a small code sample which reproduce the problem: 这是一个重现此问题的小代码示例:

<script language="javascript">
    $(document).ready(function () {
        ValidateElements();
    });

    function ValidateElements() {
            jQuery.validator.messages.required = "   ";
            jQuery.validator.messages.email = "   ";

            var rules = {};
            rules[$("#<%=TxtInvoiceName.ClientID%>").attr("name")] = {
                required: true

            };

            $('form').validate({
                rules: rules,
                success: function (label) {
                },
                errorPlacement: function (label) {

                }

            });

            $("#MainForm").validate().form();
        }
</script>

<style>
    input.error {
     border: 1px solid red;
    }
</style>

<form id="MainForm" runat="server">
<div>
    <asp:TextBox runat="server" ID="TxtInvoiceName" Text="" />
                <asp:RequiredFieldValidator ID="RequiredFieldInvoiceName" runat="server" ErrorMessage=""
                    Display="Dynamic" ControlToValidate="TxtInvoiceName"></asp:RequiredFieldValidator>
                <asp:Label runat="server" ID="LblTxtInvoiceNameValidate" AssociatedControlID="TxtInvoiceName">&nbsp;&nbsp;&nbsp;</asp:Label>
                <asp:Button runat="server" Text="PostBack" OnClick="PostBack"  />
</div>
</form>

Hope someone can point me to what im doing wrong. 希望有人可以指出我的错误所在。

You just have to add a little script on your page to replace all the validation rules after a post back 您只需在页面上添加一个小脚本即可在回发后替换所有验证规则

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, args) {
  ValidateElements();
}

This will put back your validation rules for the next form submit. 这将为下一次提交表单放回验证规则。

I've modified your ValidateElements function a bit, try this: 我已经稍微修改了ValidateElements函数,请尝试以下操作:

        function ValidateElements() {
                jQuery.validator.messages.required = "   ";
                jQuery.validator.messages.email = "   ";

                var rules = {};
                rules[$("#<%=TxtInvoiceName.ClientID%>").attr("name")] = {
                    required: true

                };

                var $form = $('#MainForm');
                $form.validate({
                    rules: rules,
                    success: function (label) {
                    },
                    errorPlacement: function (label) {

                    }

                });

                // whenever an input element's blur event fires,
                // revalidate the form. do this because the asp.net
                // validators use this behavior
                $form.find(':input').blur(function() {
                    $form.valid();
                });

                  // revalidate the form on submit.
                  // better approach because it requires less form revalidation
                  // on the client side, but it won't be in "synch" with the
                  // asp.net client side validation
//                $('#<%= Button1.ClientID %>').click(function() {
//                    return $form.valid();
//                });
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM