jQuery plugin Validate : revalidate as typing / mo

2019-08-22 05:04发布

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?

2条回答
我想做一个坏孩纸
2楼-- · 2019-08-22 05:19

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
查看更多
女痞
3楼-- · 2019-08-22 05:24

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
});
查看更多
登录 后发表回答