asp.net - 禁用验证器但验证器标注仍然显示并导致验证

标签 asp.net javascript validation

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

PS:没有 validatorCalloutExtender 一切正常,但我真的需要 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>

当对产品进行数据绑定(bind)时:

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;
    }
}

最佳答案

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 调试器找到我们正确的索引。

关于asp.net - 禁用验证器但验证器标注仍然显示并导致验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1050054/

相关文章:

asp.net - AJAX 和 FormsAuthentication,如何防止 FormsAuthentication 覆盖 HTTP 401?

javascript - 如何在 div 中适应动态 PHP 表单(以适应 fullpage.js)?

javascript - 如何一次验证表单输入(不是一一验证)

asp.net-mvc-4 - 如何在 ASP.NET MVC 4 中使用 Request.Unvalidated?

c# - 验证文本框以仅允许数值

asp.net - 我可以访问 global.asax.cs 中的虚拟目录名称吗?

javascript - MailTo 在新窗口 IE 8 中使用浏览器选项

asp.net - "web"CurrentSessionContext/ISessionFactory.GetCurrentSession 周围的线程安全

javascript - mootools:如何在单个页面中放置 10,000 个 <div> 标签

javascript - 将数据存储在数组或对象中哪个更有效?