Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Validation - Disable onfocusout for one field only

Is there a way to ignore "onfocusout" property for one form field out of 5 using Jquery validation plugin?

I mean, Jquery validation plugin currently validates an element onblur of that form field. I would like the plugin to not do so for one form field only. It can keep that validation for other form fields.

How can I do this?

Thanks,

like image 803
Jake Avatar asked Aug 16 '12 19:08

Jake


1 Answers

You can override onfocusout event handler in the options you pass to the .validate() method. The overriden version should which element is being handled, falling back to the default behaviour if it is not the field you want to exclude:

$('#test').validate({
    rules: {
        "field1": "required",
        "field2": "required",
        "field3": "required"
    },
    onfocusout: function (element, event) {
        if (element.name !== "field2") {
            $.validator.defaults.onfocusout.call(this, element, event);
        }
    }
});

See this jsFiddle for full demo.

like image 83
Infeligo Avatar answered Nov 03 '22 01:11

Infeligo