Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...