【问题标题】:Fire only one Custom Validator at a time一次只触发一个自定义验证器
【发布时间】:2012-12-19 16:50:44
【问题描述】:

我有两个Custom Validators 附加到一个文本框。文本框用于输入逗号分隔的电子邮件列表。第一个验证器检查没有重复的电子邮件地址。第二个自定义验证器检查所有电子邮件地址的格式是否有效。我有以下代码,它工作正常。

但是,当满足两个失败标准时,它们都会触发,并且我在文本框的右侧看到两个 *。一次只显示一个* 的最佳方法是什么?

代码

<asp:CustomValidator ID="valEmailRecipients" runat="server" ControlToValidate="txtEmailRecipients"
ClientValidationFunction="isUniqueElements" OnServerValidate="IsUniqueEmail_Validate"
Text="*" ErrorMessage="There should be no duplicate email address for a report"
ValidationGroup="Save">
</asp:CustomValidator>

<asp:CustomValidator ID="valEmailFormat" runat="server" ControlToValidate="txtEmailRecipients"
ClientValidationFunction="checkEmailFormat" OnServerValidate="IsValidEmailFormat_Validate"
Text="*" ErrorMessage="Entries should be valid email addresses separated by comma"
ValidationGroup="Save">
</asp:CustomValidator>

参考文献:

  1. Validating with a Custom Function for ASP.NET Server Controls
  2. Enable/Disable asp validation control in javascript
  3. How to set ValidatorEnable() javascript attribute for a validator?
  4. Validator enable using Javascript

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    最简单的方法:- 如果电子邮件格式不正确,让 valEmailRecipients 返回true。也就是说,只有在其他验证成功时才会发挥作用。

    其他方式:- Disabling 和 Enabling 验证器,这可能会很痛苦。

    只是出于好奇,为什么 2 个星号有问题?

    【讨论】:

      【解决方案2】:

      创建一个自定义验证器,并在其上执行所有验证逻辑。

      如果一个验证没有通过,设置相应的错误信息,设置 args.IsValid 为 false 并结束方法。

      在提交按钮上,记得勾选 Page.IsValid。

      【讨论】:

        猜你喜欢
        • 2012-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多