我正在尝试将自定义字段验证器添加到一些遗留代码。如果电子邮件不属于某个域,它应该阻止表单触发。
这是我的标记。
<asp:Textbox id="tbEmail" autocomplete="off" CssClass="txt260" runat="server" TabIndex="1"></asp:Textbox>
<asp:RequiredFieldValidator id="rfvEmail" runat="server" Display="Dynamic" ForeColor="" ControlToValidate="tbEmail" ErrorMessage="Required" CssClass="ValidatorError">Required</asp:RequiredFieldValidator>
<asp:CustomValidator id="cfvEmail" runat="server" Display="Dynamic" ControlToValidate="tbEmail" ErrorMessage="Invalid Email" CssClass="ValidatorError" OnServerValidate="tbEmail_CustomValidate"></asp:CustomValidator>
<asp:RegularExpressionValidator id="regExpEmail" runat="server" Display="Dynamic" ForeColor="" ControlToValidate="tbEmail" ErrorMessage="Invalid" CssClass="ValidatorError"></asp:RegularExpressionValidator>
...
<asp:Button ID="bSubmit" Text=" CREATE ACCOUNT " CssClass="NewButton" runat="server" OnClientClick="DisabledUpdateButton()" OnClick="bSubmit_Click" />
这是 C# 代码:
protected void tbEmail_CustomValidate(object source, ServerValidateEventArgs args)
{
List<string> approvedEmailDomains = new List<string>()
{
"domainnameihid.com"
};
if (!approvedEmailDomains.Any(x => tbEmail.Text.EndsWith(x)))
{
args.IsValid = false;
}
}
当我写一个无效的域名时,代码落入args.IsValid = false。一旦 args.IsValid 为假,它仍然会继续触发提交按钮。必需和常规字段验证器没有。如何获取 CustomValidator 以防止按钮的 OnClick 触发或继续?
慕勒3428872
相关分类