在客户端启用/禁用RequiredValidator / CustomValidator 不触发

发布于 2024-08-30 04:51:59 字数 955 浏览 8 评论 0原文

我有一个下拉菜单,用户可以在其中选择一个国家/地区。这是必填的“字段”。

它旁边有一个名为 State 的文本字段。如果用户选择“美国”,则需要填写“州”字段。如果用户选择瑞典,则不需要国家,因为瑞典没有国家。

示例代码:

<asp:DropDownList runat="server" ID="Country"></asp:DropDownList>
<asp:RequiredFieldValidator ControlToValidate="Country"
                runat="server" Display="Static" ErrorMessage="Required field" />

<asp:TextBox runat="server" ID="State"></asp:TextBox>
<asp:CustomValidator ClientValidationFunction="DoesntGetFiredIfStateIsEmpty"
                runat="server" Display="Static" ErrorMessage="Required field" />

<!-- SO, RATHER THIS TOGETHER WITH CONDITIONAL FIRING -->
<asp:RequiredFieldValidator ControlToValidate="State"
                runat="server" Display="Static" ErrorMessage="Required field" />

我向您提出的问题是:当此 CustomValidator 为空时,如何进行火验证?

或者更简单地说:如何有条件地触发RequiredValidator?

或者最简单:如何在客户端启用/禁用RequiredValidator?

I've got a drop-down where the user selects a Country. It is a required "field".

Next to it, there is a textfield named State. If the user selects US, then the field State is required. If the user selects e.g. Sweden, the State is not required, since Sweden has no states.

Example code:

<asp:DropDownList runat="server" ID="Country"></asp:DropDownList>
<asp:RequiredFieldValidator ControlToValidate="Country"
                runat="server" Display="Static" ErrorMessage="Required field" />

<asp:TextBox runat="server" ID="State"></asp:TextBox>
<asp:CustomValidator ClientValidationFunction="DoesntGetFiredIfStateIsEmpty"
                runat="server" Display="Static" ErrorMessage="Required field" />

<!-- SO, RATHER THIS TOGETHER WITH CONDITIONAL FIRING -->
<asp:RequiredFieldValidator ControlToValidate="State"
                runat="server" Display="Static" ErrorMessage="Required field" />

My question to you is: How can I make this CustomValidator fire validation when it is empty?

Or put simplier: How can I make a RequiredValidator fire conditionally?

Or simplest: How can I enable/disable a RequiredValidator on client-side?

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

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

发布评论

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

评论(2

花开半夏魅人心 2024-09-06 04:51:59

尝试使用 javascript 执行此操作以启用和禁用验证器

ValidatorEnable(RequiredFieldValidatorId, false);

查看 我回答的这个问题

Try doing this with javascript to enable and disable validators

ValidatorEnable(RequiredFieldValidatorId, false);

Check out this question that I answered.

岁月无声 2024-09-06 04:51:59

Asp.net 有一个客户端 JavaScript 函数来管理验证器,即“ValidatorEnable”函数,

ValidatorEnable(RequiredFieldValidatorId, false);

您可以简单地使用 JavaScript 调用它,您必须将验证器对象发送给该函数(不仅仅是它的 id)。

if (x==y) {
        ValidatorEnable($('#<%=rfvFamily.ClientID %>'), false);    
    } else {
        ValidatorEnable($('#<%=rfvFamily.ClientID %>'), true);
    }

if (x==y) {
        ValidatorEnable(document.getElementById("<%=rfvFamily.ClientID %>", false);    
    } else {
        ValidatorEnable(document.getElementById("<%=rfvFamily.ClientID %>", true);
    }

完整的文档网:
http://msdn.microsoft.com/en-us/library/Aa479045# aspplusvalid_clientside

另一种方法是在 DropDownList CausesValidation="false" 中设置,以避免在更改 DropDownList 条目时验证器阻止回发。

(*) 请记住,此功能适用于客户端,要在服务器端禁用验证器,您也必须在页面回发时禁用验证器。

if (IsPostBack){
    if (x==y) {
        rfvFamily.Enabled = false;
    }
}

Asp.net has a client side javascript function to manage the validators, the "ValidatorEnable" function,

ValidatorEnable(RequiredFieldValidatorId, false);

you can call it simply using javascript, you must send the validator object to the function (not only its id).

if (x==y) {
        ValidatorEnable($('#<%=rfvFamily.ClientID %>'), false);    
    } else {
        ValidatorEnable($('#<%=rfvFamily.ClientID %>'), true);
    }

or

if (x==y) {
        ValidatorEnable(document.getElementById("<%=rfvFamily.ClientID %>", false);    
    } else {
        ValidatorEnable(document.getElementById("<%=rfvFamily.ClientID %>", true);
    }

full documnet on:
http://msdn.microsoft.com/en-us/library/Aa479045#aspplusvalid_clientside

another way is to Set in your DropDownList CausesValidation="false" to avoid that the validators block a postback when you change the DropDownList entry.

(*) Remember this function is for client side, for disabling validator in server side, you must to disable validator on page postback too.

if (IsPostBack){
    if (x==y) {
        rfvFamily.Enabled = false;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文