jQuery 验证 : How do I create a custom error message element (not just text)?

标签 jquery jquery-plugins jquery-validate

我看过很多关于如何使用 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/

相关文章:

jquery - 主干+ RequireJs-jQuery : Cannot read property 'normalize' of undefined

javascript - jquery tooltipster 插件不工作

jquery菜单插件类似于superfish

javascript - jQuery 验证更改时的选择

javascript - jQuery 验证 : Validate that one field, 或一对的两个字段都是必需的

javascript - 如果所有 child 都不可见jquery隐藏 parent

javascript - 使用 JavaScript 在 Rails 中动态更新表单

jquery - Kendo UI 验证器错误消息未正确定位

asp.net-mvc - 自定义 MVC 验证属性和 Bootstrap 格式问题

javascript - 成功后如何切换选项卡