jQuery Validation plugin custom method. Multiple parameters

BuzzBubba picture BuzzBubba · Sep 2, 2009 · Viewed 32k times · Source

I'm trying to use a custom validator with jQuery Validation plugin. However, I'm unsure how are you supposed to pass multiple arguments to a validation method? I've tried using (2,3), curly braces and combinations, but I have failed.

This is the code - ???? marks the area I need info about:

$(document).ready(function() {
        jQuery.validator.addMethod("math", function(value, element, params) {
            return this.optional(element) || value == params[0] + params[1];
        }, jQuery.format("Please enter the correct value for {0} + {1}"));

        $("#form_register").validate({
            debug: true,
            rules: {
                inputEl: { required: true, math: ???? }
            },
            messages: {
                inputEl: { required: "Required", math: "Incorrect result" }
            }
        });
});

Answer

jantimon picture jantimon · Sep 2, 2009

Javascript arrays:

[value1,value2,value3]

SO your code might be:

inputEl: { required: true, math: [2, 3 ,4 , 5] }