在客户端启用/禁用RequiredValidator / CustomValidator 不触发
我有一个下拉菜单,用户可以在其中选择一个国家/地区。这是必填的“字段”。
它旁边有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 javascript 执行此操作以启用和禁用验证器
查看 我回答的这个问题。
Try doing this with javascript to enable and disable validators
Check out this question that I answered.
Asp.net 有一个客户端 JavaScript 函数来管理验证器,即“ValidatorEnable”函数,
您可以简单地使用 JavaScript 调用它,您必须将验证器对象发送给该函数(不仅仅是它的 id)。
或
完整的文档网:
http://msdn.microsoft.com/en-us/library/Aa479045# aspplusvalid_clientside
另一种方法是在 DropDownList CausesValidation="false" 中设置,以避免在更改 DropDownList 条目时验证器阻止回发。
(*) 请记住,此功能适用于客户端,要在服务器端禁用验证器,您也必须在页面回发时禁用验证器。
Asp.net has a client side javascript function to manage the validators, the "ValidatorEnable" function,
you can call it simply using javascript, you must send the validator object to the function (not only its id).
or
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.