Uuid
Uuid

Reputation: 2546

jQuery Validation Plugin, Unbind Single Rule?

is there way to unbind a rule in jquery validator?

suppose I have:

$('#myform').validate({
            rules: {
                field1: {
                    required: true
                },
                field2: {
                    required: true
                }
            }
        })

What I want is that when I finish the validation it must unbind field 2.

Upvotes: 0

Views: 707

Answers (2)

Sparky
Sparky

Reputation: 98738

$('#myform').validate({  // initialize the plugin
    rules: {
        field1: {
            required: true
        },
        field2: {
            required: true
        }
    }
})

You would use the .rules() method to remove all validation rules from your specified input.

$('input[name="field2"]').rules('remove')

I'm not sure I understand why you need to remove the rule after successful validation, but you could test the form programatically using the .valid() method, and then remove the rule.

if ($('#myform').valid()) {    // if the form is valid
    $('input[name="field2"]').rules('remove');  // then remove the rules
}

.valid() will immediately test the form and return a boolean true or false depending on validation status.

Upvotes: 1

Akhlesh
Akhlesh

Reputation: 2399

Use $("#field2").rules("remove", "required")

$("#commentForm").validate({
 rules: {
    field1: {
        required: true
    },
    field2: {
        required: true
    }

 }
});

//To remove rule after submitting it will be removed
$('#commentForm').submit(function () {
  $(this).validate();
  $("#field2").rules("remove", "required");
});

http://jsfiddle.net/Af9a9/3/

Upvotes: 0

Related Questions