[Solved] Why does jQuery Validate plugin do validation without calling validate()?


You’re going to need to provide a lot more detail about what you’re trying to do, but I can answer your question as you’ve written it so far.

Title: why does jquery validate without calling validate?

.validate() is called once to initialize the plugin on the form. It is not a method for triggering validation on demand. Once initialized, the form will automatically validate as per the various event callbacks built into the plugin. More details follow…

OP: i have just give option to the validate (errorElement, …), and i set
the rules, without calling validate method, or associating it with any
event, but jquery validates automatically, how to control when it
validates ?

You do not need to associate the plugin with any events because the plugin already does this automatically. It would be pretty useless if it didn’t.

When you call .validate(), you are initializing the plugin on the form. So, yes, this means the form is now ready and it will automatically do a validation test triggered on all common events such as submit, onkeyup, onfocusout, and select, button & checkbox click. Why would you not want that?

To perform a validation test programmatically, you can call the built-in .valid() method which is very much like the submit event in that it will trigger the validation test on the entire form and the form will display any errors. .valid() will also return a boolean true or false indicating if the form passes the validation test.

If you need to trigger a validation test only on a single element, rather than the entire form, you would use $('#myelement').valid(). Again, any message is displayed and a boolean value returned.

If for whatever reason you need to actually “disable” and “enable” validation completely, you can not directly do this, as there is no built-in method for un-binding the plugin from the form. However, you can simulate it by using the built-in rules('remove') and rules('add') methods. Without any rules on any fields, it will simply behave as if there’s no validation at all. However, you would still be able to use other plugin options like the submitHandler callback, just in case you are using ajax to do the data submission.

3

solved Why does jQuery Validate plugin do validation without calling validate()?