【问题标题】:Disable Validator but Validator Callout still shows and causes validation禁用验证器,但验证器标注仍显示并导致验证
【发布时间】:2010-11-06 05:01:23
【问题描述】:

我正在尝试验证是否在转发器中的产品数量文本框中输入了某个产品增量。问题是每个产品的增量都不同,所以我需要它作为每次调用的变量来验证它(我认为你不能用自定义验证器来做),我需要它在客户端使用 ValidatorCalloutExtender .我想出的最佳解决方案是触发一个 RegEx 验证器,它将通过我自己的 javascript 评估 false(另一个验证器负责确保其有效数字)。问题是,使用 ValidatorCalloutExtender,当我禁用验证器时,它仍然将其标记为无效(文本框闪烁白色然后再次变为黄色(意味着它无效),即使我放置了 JavaScript 警报并且我知道验证器正在被禁用。任何人对这里发生的事情有任何想法吗?这是代码。谢谢!

PS:没有验证器CalloutExtender 一切正常,但我真的需要Callout Extender!

验证者:

<asp:RegularExpressionValidator ID="ProductIncrementValidator" runat="server"
  ControlToValidate="ProductQtyTxt"
  ErrorMessage="Please enter a valid increment"
  ValidationExpression="^triggerthisvalidation$"
  Enabled="false"
  Display="Dynamic"
  SetFocusOnError="true"
  ValidationGroup="productValidation">
</asp:RegularExpressionValidator>

<ajax:ValidatorCalloutExtender ID="ProductIncrementVE" runat="server"
  TargetControlID="ProductIncrementValidator"
  HighlightCssClass="validator"
  WarningIconImageUrl="~/img/blank.gif">
</ajax:ValidatorCalloutExtender>

对产品进行数据绑定时:

Dim productQtyTxt As TextBox
productQtyTxt = CType(e.Item.FindControl("ProductQtyTxt"), TextBox)

Dim incrementValidator As RegularExpressionValidator
incrementValidator = CType(e.Item.FindControl("ProductIncrementValidator"), RegularExpressionValidator)
incrementValidator.ErrorMessage = "Please enter an increment of " & product.OrderIncrement.ToString()


' Add item qty increment check
productQtyTxt.Attributes.Add("onChange", "javascript:checkIncrement('" _
  & productQtyTxt.ClientID & "', " _
  & product.OrderIncrement & ", '" _
  & incrementValidator.ClientID & "')")

Javascript:

    function checkIncrement(textboxID, incrementQty, validatorID) {
    var textbox = $get(textboxID);
    var incrementValidator = $get(validatorID);
    var qtyEntered = textbox.value;

    if ((qtyEntered % incrementQty) != 0) {
        ValidatorEnable(incrementValidator, true);
        alert("not valid");
        return;
    }
    else {
        ValidatorEnable(incrementValidator, false);
        alert("valid");
        return;
    }
}

【问题讨论】:

  • 我在 javascript 函数中将文本框的 cssclass 设置为空白并隐藏了标注,所以现在可以使用,但是当焦点设置在框上时,它会调用验证并尝试针对我的 ^triggerthisvalidation$ 表达式进行验证对于 RegEx 验证器。我怎样才能让它不触发焦点验证?

标签: asp.net javascript validation


【解决方案1】:

1.为ValidatorCalloutExtender设置CSS类:

<style id = "style1" type="text/css"> .CustomValidator { position: relative; margin-left: -80px; margin-top: 8px; display: inherit; } </style>

<ajax:ValidatorCalloutExtender ID="ProductIncrementVE" runat="server" TargetControlID="ProductIncrementValidator" HighlightCssClass="validator" WarningIconImageUrl="~/img/blank.gif" CssClass="CustomValidator"> </ajax:ValidatorCalloutExtender>

2 。需要时使用 JavaScript 更改此 CSS 类。设置显示=无:

function alterDisplay(type) { var styleSheet, cssRule; if (document.styleSheets) { styleSheet = document.styleSheets[index1]; if (styleSheet) { if (styleSheet.cssRules) cssRule = styleSheet.cssRules[index2]; // Firefox else if (styleSheet.rules) cssRule = styleSheet.rules[index2]; // IE if (cssRule) { cssRule.style.display = type; } } } }

注意: index1 和 index2 可能与页面不同,这取决于您的声明。您可以使用 IE 调试器找到我们正确的索引。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,我用这样的方法解决了

    if ((qtyEntered % incrementQty) != 0) {
        ValidatorEnable(incrementValidator, true);
        $("#" + validatorID + "_ValidatorCalloutExtender_popupTable").show();
        alert("not valid");
        return;
    }
    else {
        ValidatorEnable(incrementValidator, false);
        $("#" + validatorID + "_ValidatorCalloutExtender_popupTable").hide();
        alert("valid");
        return;
    }
    

    希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2017-04-20
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2019-04-13
      相关资源
      最近更新 更多