0

我有以下代码。当我按下保存按钮时,三个 requiredfieldvalidators 运行良好且工作正常。但是我的自定义验证器不起作用。它根本不会触发事件。我有标准文本框和验证摘要控件。是否有任何原因可能无法正常工作?

<asp:RequiredFieldValidator runat="server" ControlToValidate="txtForename" Display="None" ErrorMessage="Must enter a valid first name." />
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtSurname" Display="None" ErrorMessage="Must enter a valid last name." />
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtEmail" Display="None" ErrorMessage="Must enter a valid e-mail address." />

<asp:CustomValidator runat="server" OnServerValidate="CheckAtLeastOnePhoneNumber" 
        ErrorMessage="Must enter at least one phone number." Display="None" 
        ValidateEmptyText="True" />

<script runat="server">
    void CheckAtLeastOnePhoneNumber(Object s, ServerValidateEventArgs e)
    {
        if (txtMobileNumber.Text.Equals("") &&
            txtWorkNumber.Text.Equals("") &&
            txtHomeNumber.Text.Equals(""))
        {
            e.IsValid = false;
        }
    }
</script>
4

2 回答 2

1

我修好了它。

自定义验证器控件是服务器端检查,因此其他验证器(客户端)首先执行。这有点误导,因为 validationsummary 控件通常会显示所有验证器错误。

假设名称、电子邮件等是有效的,只有这样它才会提交到服务器并提出验证错误。

于 2009-09-18T11:28:25.883 回答
1

That's right. That's the way it works. If you want a custom validator to also work in the browser, you need to implement your custom validation in JavaScript and define the method in the control's ClientValidationFunction property. Then it will show up with the client-side errors in the validation summary.

于 2011-04-30T14:27:52.760 回答