jQuery plugin Validate : revalidate as typing / mouse focus

佐手、 提交于 2019-12-13 03:43:39

问题


I have a form based on the validation demo of jQuery's docs : http://jsfiddle.net/ABbDS/1/ . Clicking submit or hitting enter does validate it.

How do I make tab / changing mouse focus / typing also validate? ( So they immediately know the email is

It is based off of : http://jquery.bassistance.de/validate/demo/milk/ (which does this) and http://docs.jquery.com/Plugins/Validation/

Mine isn't validating when clicking the next element or hitting tab. Answers I found did not use the validate plugin.

edit: Is this the right answer? : https://stackoverflow.com/a/9381803/341744 Does $(this).valid() call validate()'s function?


回答1:


The documentation on the jquery site concerning onfocusout and onkeyup is slightly misleading. It states that onfocusout and onkeyup are boolean values, which would lead you to believe that you could set their values to true or false. Wrong! You should only set them to false. If you set their values to true then you get this error

Uncaught TypeError: Object true has no method 'call'

This is because the default onfocusout and onkeyup are functions not boolean values. If you set the values to false then the function is not called, but if set to true then boom you get a javascript error. This type of programming is difficult for a programmer like me used to strong typing to get used to, but is pretty clever.

There is no reason to set onfocusout to be true as the default behaviour is to validate on losing focus, so just remove these two lines in your code

// remove these lines
// onfocusout:true,  
// onkeyup: true



回答2:


There are quite a few events you can add to the validator options that may help you.

Example:

onkeyup:

Validate elements on keyup. As long as the field is not marked as invalid, nothing happens. Otherwise, all rules are checked on each key up event.

$(".selector").validate({
   onkeyup: true
});


来源:https://stackoverflow.com/questions/10938431/jquery-plugin-validate-revalidate-as-typing-mouse-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!