我想使用jQuery Validate插件实时(输入)验证表单。我的其余代码工作正常,但是应该使它们验证实时的onfocusout
和onkeyup
回调函数无法正常工作。是在错误的地方还是什么地方?
这是我当前验证功能的一个示例:
$(document).ready(function(){
$(function () {
//$('input').on('change', function(){
$("form[name='valid']").validate({
rules: {
name: "required",
subject: "required",
phone: "required",
email: {
required: true,
//built in email validation
email: true
}
},
messages: {
name: "Must enter full name. ",
subject: "Must enter subject. ",
phone: "Must enter phone number. ",
email: "Must enter valid email address."
},
//real time validation
onfocusout: function(element) {
this.element(element); // triggers validation
},
onkeyup: function(element, event) {
this.element(element); // triggers validation
},
errorPlacement: function (error, element) {
error.appendTo(element.parent());
},
submitHandler: function(form) {
form.submit();
}
});
});
});