您可以使用 bassassistance 的 jquery 验证插件单独验证字段集吗? [英] Can you validate fieldsets individually using the jquery validation plugin from bassassistance?

查看:28
本文介绍了您可以使用 bassassistance 的 jquery 验证插件单独验证字段集吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用验证插件的表单,但我需要使用稍微不同的标准来验证表单的一个单独部分 - 大多数字段将错误放在下一个表格单元格中,但对于一个字段,我需要错误放在别处.

I have a form that uses the validation plugin, but I need to validate a separate part of the form using slightly different criteria - most of the fields place the error in the next table cell, but for one field I need the error placed somewhere else.

这是主要表单域的验证函数:

This is the validate function for the main form fields:

jQuery("#form2").validate({
    rules: {
        street: {
            required: true,
            minlength: 5
        },
        city: {
            required: true,
            minlength: 3
        },
        state: {
            required: true
        },
        zip: {
            required: true,
            minlength: 5
        }
    },
    messages: {
        street: {
            required: "Please enter your address",
            minlength: "Address is too short"
        },
        city: {
            required: "Please enter your town/city",
            minlength: "Town/City is too short"
        },
        state: {
            required: "Please enter your county"
        },
        zip: { 
            required: "Please enter your postcode",
            minlength: "Postcode is too short"
        }
    },
    errorPlacement: function(error, element) {
            error.appendTo(element.parent("td").next("td"));
    }
}); // end validate

基本上我也想单独验证这个字段集,所以可以为 errorPlacement 使用不同的值:

Basically I would also like to validate this fieldset separately, so a different value can be used for errorPlacement:

    jQuery("#elecfields").validate({
    rules: {
        sup1: {
            minlength: 2
        }
    },
    messages: {
        sup1: {
            minlength: "must be 2 digits"
        }
    },
    errorPlacement: function(error, element)
    {
        // different error placement is needed here
    }

}); // end elecfields validate

elecfields 是 form2 中的一个字段集 - 但这似乎不起作用.

elecfields is a fieldset inside form2 - but this doesn't seem to work.

似乎 errorPlacement 必须适用于表单中的所有字段,但我只有一个需要错误的字段放在其他地方.关于如何实现这一目标的任何想法?非常感谢任何帮助.

It seems that the errorPlacement must apply to all fields in the form, but I just have that one field that needs the error places somewhere else. Any ideas for how this can be achieved? Any help is very much appreciated.

推荐答案

你说得对.errorPlacement 处理程序确实适用于整个表单,但您也可以在其中设置逻辑以在某些情况下以不同方式处理错误.例如,您可以查看元素属性 direclty 并注意有问题的项目,或者您可以根据共同的父项过滤它们 - 在您的情况下是字段集.示例:

You're right. The errorPlacement handler does apply to the entire form, but you can also have logic in there to handle the error differently in certain situations. For instance, you could look at the element property direclty and watch for the items in question, or you could filter them based upon a common parent - in your case the fieldset. Example:

$("#form").validate({
    /* */
    errorPlacement: function(error, element)
    {
        if( element.closest("#subFieldSet").length ) {
            /* special handling */
        } else {
            /* generic handling */
            error.insertAfter(element);
        }
    }
});

这篇关于您可以使用 bassassistance 的 jquery 验证插件单独验证字段集吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆