我看过很多关于如何使用 message: 来自定义错误文本的示例。我想要实现的目标是不同的。
我有客户端和服务器端验证,所以我希望两者显示相同的错误消息。为了避免重复代码,似乎有 <div class="error" id="bad_email">Invalid e-mail</span>
是最好的方法。
我在验证 jQuery 插件中找不到执行此操作的方法。有什么想法吗?
最佳答案
有一种方法可以做到这一点,你应该阅读插件的文档。 从 link here 中阅读
以下是具体操作方法。
使用 invalidHandler。
$(".selector").validate({
invalidHandler: function(form, validator) {
var errors = validator.numberOfInvalids();
if (errors) {
var message = errors == 1
? 'You missed 1 field. It has been highlighted'
: 'You missed ' + errors + ' fields. They have been highlighted';
/* .... put here the call to a custom function to display validation error */
customvalidationdisplay(message,"show");
} else {
/* ... put here the call to a custom function to hide validation error if displayed */
customvalidationdisplay("hide");
}
}
});
function customvalidationdisplay(message,action){
/* write here your custom script to display the error if action = "show" or hide if action = "hide" */
}
我希望你现在已经明白这一点了。如果没有,请随时向我询问更多信息。
关于jQuery 验证 : How do I create a custom error message element (not just text)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3145708/