JQueryPlacement validation failed

It works:

$("[id$='_zzz']").rules(
    "add",
    {
        required: true,
        minlength: 8,
        messages: {
            required: "...",
            minlength: jQuery.format("...")
        }            
    }
);

      

An error message will appear.

When I try to style the message, this doesn't work:

$("[id$='_zzz']").rules(
    "add",
    {
        required: true,
        minlength: 8,
        messages: {
            required: "...",
            minlength: jQuery.format("...")
        },
        errorElement: "span",
        errorPlacement: function(error, element) {
            error.insertAfter(element);
            error.css("margin", "0 0 0 5px");
        }             
    }
);

      

When I style with the validate function, the style is applied so that it works:

$('#aspnetForm').validate({
    errorElement: "span",
    errorPlacement: function(error, element) {
        error.insertAfter(element);
        error.css("margin", "0 0 0 5px");
    }
});

      

Why can't I use the replace style with the add add function?

+2


a source to share


1 answer


In the first method, you pass an errorPlacement

object property / function to the rules method ... it just doesn't care about it and doesn't use it. For any object you pass in javascript, it must check this property or use it in some way, otherwise it is just extraneous information. You can also add myExtrathing:'ValueHere'

to object, won't break anything ... but also won't be used.



The method .validate()

has an existing functionerrorPlacement

(which makes heavy use of that important part, it looks for it) in the default parameters and does an extension to replace the default method with the one you provided ... if you provided it. The same behavior is true for almost every jQuery plugin and any parameter you specify, it combines the default parameters with the ones you provide / override. You can see some simple examples of this behavior in the jQuery plugin tutorial .

+3


a source







All Articles