【发布时间】:2019-03-08 09:20:39
【问题描述】:
我正在尝试使用 CustomValidator 来验证输入的字符串 - po box
基本上,我希望用户避免在地址框中输入他们的邮政地址,因为我已经使用正则表达式为其编写了函数。但问题是,虽然它触发了该功能,但并没有出现错误消息。 (我知道这一点是因为我通过使用 Chome 开发人员工具在我的 Javascript 函数中放置断点来检查这一点。)
下面是我的 .ascx 文件代码。
<div class="clearfix">
<asp:Label ID="lblAddress" runat="server" Text="Address" AssociatedControlID="txtAddress"
class="formlabel firstfocus"></asp:Label>
<div class="input">
<asp:TextBox ID="txtAddress" runat="server" CssClass="large" MaxLength="80"></asp:TextBox>
<asp:RequiredFieldValidator ID="valAddress" runat="server" ControlToValidate="txtAddress"
CssClass="error-block" Display="Dynamic" ErrorMessage="<p>Address is required</p>"
SetFocusOnError="True"></asp:RequiredFieldValidator>
<asp:CustomValidator ID="valAddressPOBOX" runat="server" EnableClientScript="true" ValidateEmptyText="true" ErrorMessage="<p>Cannot contain a PO Box number</p>" ClientValidationFunction="AddressPOBoxValidation" ControlToValidate="txtAddress" Display="Dynamic" SetFocusOnError="True"></asp:CustomValidator>
</div>
</div>
RequiredFieldValidator 触发错误消息。
下面是显示由 RequiredFieldValidator 触发的错误消息的屏幕截图
使用 CustomValidator 会触发,但不会出现错误消息。我已经在同一个 ascx 页面上检查了其他 CustomValidators 并确保这个具有与它们没有不同的属性,但是,如果我缺少任何可见性属性,请随时纠正我。
以下是 2 个屏幕截图,展示了 CustomValidator 的效果如何发生。
进入前邮箱
进入po box后(注意地址和地址2之间的差距是如何增加的。)
来到 Javascript 端,下面是函数
function CheckPOBOXAddress(address) {
var poboxPattern = /(p\.?\s?o?\.?\s?b\.?(ox)\.?(\s|[0-9])?|post\soffice)/i;
if (poboxPattern.test(address))
return true;
else
return false;
}
function AddressPOBoxValidation(sender, args) {
var address = args.Value;
if (CheckPOBOXAddress(address)) {
args.IsValid = false;
return;
}
args.IsValid = false;
}
【问题讨论】:
标签: javascript asp.net-mvc-4 customvalidator