自定义验证器问题

发布于 2024-10-16 19:04:44 字数 2160 浏览 2 评论 0原文

我的页面上有许多不同类型的 ASP.NET 验证器,其中一些验证器我根据页面上的用户选择使用 javascript 禁用。

$.each(Page_Validators, function(index, validator) 
       {
           if ($(validator).hasClass("pain")) {

               ValidatorEnable(validator, false);

        }
});

这似乎有效,并且验证器在所有情况下都不会触发,除了我在无法使用其他验证器类型时使用的任何 CustomValidators 之外

 <asp:CheckBoxList id="BodyPartsList" runat="server" RepeatColumns = "2" 
                    Width="1023px">
                    <asp:ListItem Text = "0. Head/headaches" Value = "1"></asp:ListItem>
                    <asp:ListItem Text = "1. Leg(s)" Value = "2"></asp:ListItem>
                    <asp:ListItem Text = "2. Arm(s)" Value = "3"></asp:ListItem>
                    <asp:ListItem Text = "3. Neck" Value = "4"></asp:ListItem>
                    <asp:ListItem Text = "4. Shoulder(s)" Value = "5"></asp:ListItem>
                    <asp:ListItem Text = "5. Low Back" Value = "6"></asp:ListItem>
                    <asp:ListItem Text = "6. Upper Back" Value = "7"></asp:ListItem>
                    <asp:ListItem Text = "7. Feet" Value = "8"></asp:ListItem>
                    <asp:ListItem Text = "8. Hand(s)" Value = "9"></asp:ListItem>
                    <asp:ListItem Text = "9. Other(Describe in &quot;Details of Plan&quot;)" Value = "10"></asp:ListItem>
                </asp:CheckBoxList>

                <asp:CustomValidator ID="PainLocationValidator" runat="server" Display="Dynamic"
                ErrorMessage="Location of pain is required." 
                ClientValidationFunction="checkPainListValidate" 
                ValidationGroup = "OnSave" EnableClientScript= "true" SetFocusOnError= "true" Width = "100%" CssClass = "pain" />



 function checkPainListValidate(sender, args) {

    args.IsValid = true;

    if ($('#<%= BodyPartsList.ClientID %> input:checked').length > 0)
        args.IsValid = true;

    else
        args.IsValid = false;

}

为什么会发生这种情况?我还能做些什么来禁用这些功能吗?

谢谢。

I have a number of ASP.NET validators on my page of various types, some of which I am disabling depending on user selections on a page, using javascript.

$.each(Page_Validators, function(index, validator) 
       {
           if ($(validator).hasClass("pain")) {

               ValidatorEnable(validator, false);

        }
});

This seems to work and validators do not fire in all cases except any CustomValidators that I am using whenever I cannot use other validator types

 <asp:CheckBoxList id="BodyPartsList" runat="server" RepeatColumns = "2" 
                    Width="1023px">
                    <asp:ListItem Text = "0. Head/headaches" Value = "1"></asp:ListItem>
                    <asp:ListItem Text = "1. Leg(s)" Value = "2"></asp:ListItem>
                    <asp:ListItem Text = "2. Arm(s)" Value = "3"></asp:ListItem>
                    <asp:ListItem Text = "3. Neck" Value = "4"></asp:ListItem>
                    <asp:ListItem Text = "4. Shoulder(s)" Value = "5"></asp:ListItem>
                    <asp:ListItem Text = "5. Low Back" Value = "6"></asp:ListItem>
                    <asp:ListItem Text = "6. Upper Back" Value = "7"></asp:ListItem>
                    <asp:ListItem Text = "7. Feet" Value = "8"></asp:ListItem>
                    <asp:ListItem Text = "8. Hand(s)" Value = "9"></asp:ListItem>
                    <asp:ListItem Text = "9. Other(Describe in "Details of Plan")" Value = "10"></asp:ListItem>
                </asp:CheckBoxList>

                <asp:CustomValidator ID="PainLocationValidator" runat="server" Display="Dynamic"
                ErrorMessage="Location of pain is required." 
                ClientValidationFunction="checkPainListValidate" 
                ValidationGroup = "OnSave" EnableClientScript= "true" SetFocusOnError= "true" Width = "100%" CssClass = "pain" />



 function checkPainListValidate(sender, args) {

    args.IsValid = true;

    if ($('#<%= BodyPartsList.ClientID %> input:checked').length > 0)
        args.IsValid = true;

    else
        args.IsValid = false;

}

Why is this happening? Is there something else I can do to disable those as well?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

行雁书 2024-10-23 19:04:44

一些 aspx 标记会很有帮助,但无论如何,这里有一些想法:

  • 您是否调试了 javascript 以查看发生了什么,如果您的验证器为 null 或者您有类似 validatorEnable 而不是 ValidatorEnable 的类型?您收到任何 JavaScript 错误吗?
  • CustomValidator 是在服务器端还是在客户端验证,我认为 ValidatorEnable 只会在客户端禁用它,
  • EnableClientScript 设置为 true
  • 如果您不指定 ControlToValidate,则 CustomValidator 将在每次回发时进行验证,无论事件如何导致它的原因
  • 您是否提供了在客户端调用的 ClientValidationFunction
  • 顺便问一下,您使用的是什么浏览器和什么框架,是ASP.Net-Ajax吗?
  • 您是否尝试过通过 document.getElementById('<%=MyValidator.ClientID %>').disabled=true; 禁用它?

我假设您使用了错误的 ID 在客户端获取验证器。

我已经测试了您的代码,但无法重现此行为:

这是我的 Javascript 函数,用于禁用所有验证器(经过编辑以考虑您的痛苦类别):

 function disablePainValidators() {
     if ((typeof (Page_Validators) != "undefined") && (Page_Validators != null)) {
         var i;
         for (i = 0; i < Page_Validators.length; i++) {
             if(Page_Validators[i].className=='pain')
                 ValidatorEnable(Page_Validators[i], false);
         }
     }
 }

Some aspx-markup would be helpful, but anyway here are some ideas:

  • have you debugged the javascript to see what's happening, if your validator is null or you have a type like validatorEnable instead of ValidatorEnable? Do you get any javascript errors?
  • Does the CustomValidator validates on server- or on clientside, i think ValidatorEnable will only disable it on clientside
  • set EnableClientScript to true
  • If you dont specify a ControlToValidate, the CustomValidator will validate on every postback, regardless of the event that caused it
  • Have you provided a ClientValidationFunction that gets called on clientside?
  • by the way, what browser and what framework are you using, is it ASP.Net-Ajax?
  • do you have tried to disable it via document.getElementById('<%=MyValidator.ClientID %>').disabled=true;?

I assume that you're using the wrong ID to get the validator in clientside.

I have tested your code and can not reproduce this behaviour:

This is my Javascript-function to disable all validators(edited to take your pain-class into account):

 function disablePainValidators() {
     if ((typeof (Page_Validators) != "undefined") && (Page_Validators != null)) {
         var i;
         for (i = 0; i < Page_Validators.length; i++) {
             if(Page_Validators[i].className=='pain')
                 ValidatorEnable(Page_Validators[i], false);
         }
     }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文