-1

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
  • Your scenario is impossible as presented. Rules must be assigned to fields, otherwise, no rules are used. It would be impossible for all three rules to be called on one field unless you assigned all three rules to that one field. This is a poorly written question because it's lacking enough code to reproduce the issue. Please review the FAQ section about posting good questions. Commented Aug 15, 2023 at 22:02

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.