jQuery Validate trigger only one custom method

42 Views Asked by At

How to trigger only one specific custom validation? Let's say I have three custom validation methods:

$.validator.addMethod('customValidation1', function (data)
{
 }, 'Error');
$.validator.addMethod('customValidation2', function (data)
{
 }, 'Error');
$.validator.addMethod('customValidation3', function (data)
{
 }, 'Error');

Insted of calling all of them, how can I trigger just customValidation1 without triggering customValidation2 and customValidation3.

Calling something like this$("#id").valid(); is triggring all of them.

1

There are 1 best solutions below

0
Sparky On

Quote OP:

calling something like this $("#id").valid(); is triggering all of them.

I don't see how that is possible. Did you somehow assign all three custom rules to the #id field? Otherwise, fields can not automatically pick up any of your custom rules without a programmatic declaration.

$(document).ready(function() {

    $("#myform").validate({  // <-- initialize plugin on the form.

        rules: {
            field_name: {  // <-- this is the name attribute, NOT id
                required: true,
                customValidation2: param  // <-- declare custom rule on this field
            }
        }

    });

});

Let's assume that the field with the name="field_name" also has id="field_name".

<input type="text" name="field_name" id="field_name" />

Calling something like $("#field_name").valid() will only evaluate the customValidation2 method along with any other rules or methods as declared in the .validate() example above.