一次仅触发一个自定义验证器

时间:2012-12-19 16:50:44

标签: c# asp.net

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

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

enter image description here

CODE

<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

2 个答案:

答案 0 :(得分:2)

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

其他方式: - DisablingEnabling验证器,这可能很痛苦。

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

答案 1 :(得分:1)

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

如果未通过一个验证,请将相应的错误消息设置为它,将args.IsValid设置为false并结束该方法。

在提交按钮上,记得检查Page.IsValid。

相关问题