Can you check individual fields separately with the jquery validation plugin from the bassassistant?

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

This is the function of validating the fields of the main forms:

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

Basically, I would also like to check this set of fields separately, so for errorPlacement you can use a different value:

    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 is a collection of fields inside form2, but this does not seem to work.

, errorPlacement , , - . , ? .

+3
2

. errorPlacement , - . , direclty , - - . :

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

?

, , errorPlacement.

0

Source: https://habr.com/ru/post/1728909/


All Articles