使用 jquery.validationEngine.js 进行验证时如何忽略隐藏的页面元素?

发布于 2024-10-03 09:48:50 字数 858 浏览 1 评论 0原文

我有一个 div,其样式默认为“display: none”,直到填写了前一个字段,并且它包含一个需要选择的下拉列表。

如果在此 div 可见之前提交页面,则验证错误将显示在页面的左半部分。

当然,这不是一个功能问题,但我一直想知道是否有办法让验证引擎忽略隐藏元素。

到目前为止,我已经尝试过 class="validate[optional: Special]" 正如 作者博客: "可选:特殊:仅在字段不为空时验证*请先调用可选"。它似乎没有按照建议工作。

<div id="container" style="display: none;">
   ...
   <select id="mapLocation" onchange="moveMapToCenter();" class="validate[optional: Special]" />
   ...
</div> 

我还尝试过使用 jquery.validate“ignore”:

$(document).ready(function() {
   $("#aspnetForm").validationEngine({
      ignore: ":hidden"
      success: false,
      failure: function() {}
   })
});

这可能是我的一个简单的疏忽,我们拭目以待!谢谢。

I have a div who's style defaults to "display: none" until a previous field has been filled out, and it contains a drop down list that requires a selection.

If the page is submitted before this div becomes visible, the validation error appears to the left half-off the page.

This is not a functional problem of course, but I've been wondering if there's a way to have the validation engine ignore hidden elements.

So far I've tried class="validate[optional: Special]" as pointed out on the creator's blog: "optional: Special: Only validate when the field is not empty *Please call optional first". It doesn't seem to work as suggested.

<div id="container" style="display: none;">
   ...
   <select id="mapLocation" onchange="moveMapToCenter();" class="validate[optional: Special]" />
   ...
</div> 

I've also tried using jquery.validate "ignore":

$(document).ready(function() {
   $("#aspnetForm").validationEngine({
      ignore: ":hidden"
      success: false,
      failure: function() {}
   })
});

This may be a simple oversight on my part, we'll see! Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

皇甫轩 2024-10-10 09:48:50

我最终通过隐藏和显示元素的函数添加和删除validation[require]类属性来解决这个问题。

function showContainer() {
   ...
   $("#mapLocation").addClass("validate[required]");
   ...
}
function hideContainer() {
   ...
   $("#mapLocation").removeClass("validate[required]");
}

当隐藏需要验证的页面元素时,这可以很好地避免验证错误消息出现在奇怪的地方。

I ended up solving this by adding and removing the validation[required] class attribute through functions that hide and show the element.

function showContainer() {
   ...
   $("#mapLocation").addClass("validate[required]");
   ...
}
function hideContainer() {
   ...
   $("#mapLocation").removeClass("validate[required]");
}

This works fairly well to avoid validation error messages showing up in strange places when hiding page elements that require validation.

书间行客 2024-10-10 09:48:50

我需要禁用临时更多字段,我做了这个,一个很好的解决方案:
一个小的 jquery 扩展:

jQuery.extend(jQuery.fn, {
    switchValidationEngine: function (enable) {
        $.each(this, function (i, n) {
            var field = $(n);
            var rulesParsing = field.attr("class");
            var newVal = enable ? rulesParsing.replace(/validate_not\[/g, "validate[") : rulesParsing.replace(/validate\[/g, "validate_not[");
            field.attr("class", newVal);
        });
    }
});

用于禁用字段验证:

$("#txtModel").switchValidationEngine(false);

重新启用字段验证的用法:

$("#txtModel").switchValidationEngine(true);

i need disable temporary more fields, i make this, a pretty solution:
a little jquery extension:

jQuery.extend(jQuery.fn, {
    switchValidationEngine: function (enable) {
        $.each(this, function (i, n) {
            var field = $(n);
            var rulesParsing = field.attr("class");
            var newVal = enable ? rulesParsing.replace(/validate_not\[/g, "validate[") : rulesParsing.replace(/validate\[/g, "validate_not[");
            field.attr("class", newVal);
        });
    }
});

usage to disable a field validation:

$("#txtModel").switchValidationEngine(false);

usage to re-enable a field validation:

$("#txtModel").switchValidationEngine(true);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文