有一个简单的方法来改变默认的错误值在jQuery验证插件?

我只是想重写错误消息,以使我的应用程序更加个性化——我有很多字段,所以我不想为字段x单独设置消息……我知道我能做到!


当前回答

这招对我很管用:

$.validator.messages.required = 'required';

其他回答

另一种可能的解决方案是遍历字段,向每个字段添加相同的错误消息。

$('.required').each(function(index) {
  $(this).rules("add", {
    messages: {
      required: "Custom error message."
   }
  });
});

您可以在验证调用中指定自己的消息。从验证插件文档(http://jquery.bassistance.de/validate/demo/milk/)中使用的Remember the Milk注册表单中提升和缩写这段代码,您可以轻松地指定自己的消息:

var validator = $("#signupform").validate({
    rules: {
        firstname: "required",
        lastname: "required",
        username: {
            required: true,
            minlength: 2,
            remote: "users.php"
        }
    },
    messages: {
        firstname: "Enter your firstname",
        lastname: "Enter your lastname",
        username: {
            required: "Enter a username",
            minlength: jQuery.format("Enter at least {0} characters"),
            remote: jQuery.format("{0} is already in use")
        }
    }
});

验证的完整API(…):http://jqueryvalidation.org/validate

这招对我很管用:

// Change default JQuery validation Messages.
$("#addnewcadidateform").validate({
        rules: {
            firstname: "required",
            lastname: "required",
            email: "required email",
        },
        messages: {
            firstname: "Enter your First Name",
            lastname: "Enter your Last Name",
            email: {
                required: "Enter your Email",
                email: "Please enter a valid email address.",
            }
        }
    })

删除所有默认错误消息

$.validator.messages.required = "";

这招对我很管用:

$.validator.messages.required = 'required';