我正在使用jquery验证插件。点击“提交”按钮后,我的表单得到验证。但我需要onblur验证,例如:如果html文本框/其他元素失去了他们的焦点(onblur),那么我需要验证相应的字段。那么如何修改以下代码呢?
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript" src="jquery.validate.js"></script>
<script type="text/javascript" >
$(document).ready(function() {
$.validator.addMethod("email", function(value, element)
{
return this.optional(element) || /^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/i.test(value);
}, "Please enter a valid email address.");
$.validator.addMethod("username",function(value,element)
{
return this.optional(element) || /^[a-zA-Z0-9._-]{3,16}$/i.test(value);
},"Username are 3-15 characters");
$.validator.addMethod("password",function(value,element)
{
return this.optional(element) || /^[A-Za-z0-9!@#$%^&*()_]{6,16}$/i.test(value);
},"Passwords are 6-16 characters");
// Validate signup form
$("#signup").validate({
rules: {
email: "required email",
username: "required username",
password: "required password",
},
});
});
</script>
提前致谢
答案 0 :(得分:11)
根据docs,您可以在输入因onfocusout
失去焦点时启用验证:
$(".selector").validate({
onfocusout: false
})
在您的情况下,特别是:
$("#signup").validate({
onfocusout: false, //this setting is what you need
rules: {
email: "required email",
username: "required username",
password: "required password"
}
});