小编典典

jQuery 验证:更改默认错误消息

all

有没有一种简单的方法来更改jQuery 验证插件中的默认错误值?

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


阅读 64

收藏
2022-03-25

共1个答案

小编典典

将此代码添加到验证插件之后 包含的单独文件/脚本中以覆盖消息,随意编辑:)

jQuery.extend(jQuery.validator.messages, {
    required: "This field is required.",
    remote: "Please fix this field.",
    email: "Please enter a valid email address.",
    url: "Please enter a valid URL.",
    date: "Please enter a valid date.",
    dateISO: "Please enter a valid date (ISO).",
    number: "Please enter a valid number.",
    digits: "Please enter only digits.",
    creditcard: "Please enter a valid credit card number.",
    equalTo: "Please enter the same value again.",
    accept: "Please enter a value with a valid extension.",
    maxlength: jQuery.validator.format("Please enter no more than {0} characters."),
    minlength: jQuery.validator.format("Please enter at least {0} characters."),
    rangelength: jQuery.validator.format("Please enter a value between {0} and {1} characters long."),
    range: jQuery.validator.format("Please enter a value between {0} and {1}."),
    max: jQuery.validator.format("Please enter a value less than or equal to {0}."),
    min: jQuery.validator.format("Please enter a value greater than or equal to {0}.")
});
2022-03-25