Question

I'm trying to add a class rule instead of a name rule in jquery validate but cannot seems to get the aspect of it.

    form.validate({
        //NAME RULE
        rules: {
            //profile
            first: {
                minlength: 2,
                required: true
            },

        },
       //CLASS RULE 
       addClassRules: {
            receive: {
                minlength: 2,
                required: true
            },
        },

    });

am i doing this correctly?

Reference : http://jqueryvalidation.org/jQuery.validator.addClassRules/

Was it helpful?

Solution

The addClassRules method does not belong inside of .validate().

form.validate({
    //NAME RULE
    rules: {
        //profile
        first: {
            minlength: 2,
            required: true
        }
    }
});

As per documentation, it gets attached to the validator object to create the compound rule that represents the standard rules specified inside.

jQuery.validator.addClassRules("myCompundRule", {
    required: true,
    minlength: 2
});

Usage is as simple as applying the new class to your input.

<input type="text" class="myCompundRule" name="somename" />

OTHER TIPS

 $('input[id*="vital"]').each(function () {
    sr=$(this).attr("data-startrange");
    //console.log(sr);
    er=$(this).attr("data-endrange");
    $('.addEnc').rules('add', {
        required: true,
         range:[sr,er]

    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top