jQuery 验证(至少一个字段有文本)

发布于 2024-09-18 10:09:20 字数 272 浏览 4 评论 0原文

我的这个页面有几个 TextAreas (asp.net mvc)。
我需要验证这些字段中至少有一个包含文本。 我怎样才能用 jQuery 做到这一点?

<%= Html.TextArea("taMetodologia",Model.Enmienda.Detalles.EnmiendaMetodologia, 8, 70,new {@class = "Especificaciones"}) %>

这是我拥有的 textAreas 的一个示例。

I have this page which has several TextAreas (asp.net mvc).
I need to validate that at least one of those fields has text in it.
How can I do that with jQuery?

<%= Html.TextArea("taMetodologia",Model.Enmienda.Detalles.EnmiendaMetodologia, 8, 70,new {@class = "Especificaciones"}) %>

That's an example of the textAreas I have.

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

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

发布评论

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

评论(2

遇见了你 2024-09-25 10:09:20

假设您没有使用验证插件并且所有文本区域都有“Especaciones”类,您可以在该类上使用过滤器。

如果以下内容的长度大于 0,则至少有一个文本区域具有值。

$(".Especificaciones").filter(function(){
    return $(this).val() != "";
}).length;

Assuming you are not using the validate plugin and all the textareas have the "Especificaciones" class, you could use a filter on the class.

If the length of the following is greater than 0, then at least one of the textareas had a value in it.

$(".Especificaciones").filter(function(){
    return $(this).val() != "";
}).length;
—━☆沉默づ 2024-09-25 10:09:20

查看此链接,因为它应该是您要寻找的内容:

jQuery 验证:指示一组中至少需要一个元素

引用上述链接:

首先,我输入 class=组中每个元素的“required_group”。然后,我添加了一个自定义验证方法:

jQuery.validator.addMethod('required_group', function(val, el) {
    var $module = $(el).parents('div.panel');
    return $module.find('.required_group:filled').length;
});

...自定义类规则以利用新方法:

jQuery.validator.addClassRules('required_group', {
    'required_group' : true
});

...最后是新方法的自定义消息:

jQuery.validator.messages.required_group = 'Please fill out at least one of these fields.';

这是使用此方法但对其进行了调整的人的另一个链接:

jquery 只需要验证一组中的一个字段

Check out this link as it should be what your looking for:

jQuery validation: Indicate that at least one element in a group is required

Quoted from the above link:

To start with, I put class="required_group" on each of the elements in the group. Then, I added a custom validation method:

jQuery.validator.addMethod('required_group', function(val, el) {
    var $module = $(el).parents('div.panel');
    return $module.find('.required_group:filled').length;
});

... a custom class rule to take advantage of the new method:

jQuery.validator.addClassRules('required_group', {
    'required_group' : true
});

... and finally a custom message for the new method:

jQuery.validator.messages.required_group = 'Please fill out at least one of these fields.';

Here is another link to someone that used this method but tweaked it:

jquery validation only one field in a group required

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