【问题标题】:Custom Validator - Error message does not display on front end自定义验证器 - 前端不显示错误消息
【发布时间】: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


    【解决方案1】:

    我找到了解决问题的方法。经过几天的试验、错误和研究,这对我有用:

    <asp:CustomValidator ID="CustomAddressValidator" runat="server" ClientValidationFunction="CheckAddressValidation" ControlToValidate="txtAddress" CssClass="error-block" Display="Dynamic" SetFocusOnError="True">Cannot contain a PO Box number</asp:CustomValidator>
    

    我还将我的 Javascript 修改为一个函数:

    function CheckAddressValidation(sender, args) {
        var address = args.Value;
        var addressPattern = /(p\.?\s?o?\.?\s?b\.?(ox)\.?(\s|[0-9])?|post\soffice)/i;
        if (addressPattern.test(address)) {
            args.IsValid = false;
            return;
        }
        args.IsValid = true;
    }
    

    课程 - CreateProfile.ascx 文件的另一部分具有与我最初发布的格式相同的自定义验证器。我的上级认为微软对此做了一些更改,这可能就是为什么在 ErrorMessage 标记中放置错误消息似乎不起作用的原因。如果我在某个地方错了,请随时纠正我。但是,如果您是陷入类似情况的人,我的解决方案应该会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多