我正在使用jQuery验证插件来验证表单数据。它在表单提交时工作正常。但onkeyup
和onblur
无效。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" ></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.js"></script>
var validator = $("#myForm").validate( {
rules:{
attribute: "required",
attributeValue: "required email"
},
messages:{
attribute: "Required",
attributeValue: {
required: "Enter email",
email: "Enter valid email"
}
}
});
我在这里做错了吗?
答案 0 :(得分:2)
$("#element").blur(function(){
$(this).validate({
// your code here
})
})
或更准确地说
$('#myForm').blur(function(){
validator.validate()
});
答案 1 :(得分:1)
Please include key up event or blur event in your validate function
var validator = $("#myForm").validate( {
//this setting for on key up event
onkeyup: function(element) { $(element).valid(); },
rules:{
attribute: "required",
attributeValue: "required email"
},
messages:{
attribute: "Required",
attributeValue: {
required: "Enter email",
email: "Enter valid email"
}
}
});
if($("#myForm").valid()) {
// call the function which you want to call after validation;
}
答案 2 :(得分:0)
在Validate函数中为onblur添加以下代码段:
onfocusout: function (element) {
$(element).valid();
}
所以它会变成如下:
var validator = $("#myForm").validate( {
rules:{
attribute: "required",
attributeValue: "required email"
},
onfocusout: function (element) {
$(element).valid();
},
messages:{
attribute: "Required",
attributeValue: {
required: "Enter email",
email: "Enter valid email"
}
}
});
现在,您将能够验证模糊输入。
答案 3 :(得分:-2)
您是否尝试在输入代码中添加类名?
<input type="text" class="require email" name="email"/>