jQuery 验证插件 - 自定义消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6777634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:22:26  来源:igfitidea点击:

jQuery Validation plugin - Custom message

jqueryjquery-validate

提问by Detonator

I am new to JQuery and am using the JQuery validation plugin.

我是 JQuery 的新手,正在使用JQuery 验证插件

I would like to ask, how to override the default messages to display also text in label associated with form element.

我想问一下,如何覆盖默认消息以在与表单元素关联的标签中显示文本。

I would get message something like this:

我会收到这样的消息:

Field Password is required.instead of default This field is required.

字段密码是必需的。而不是默认此字段是必需的

Field PostCode must contain at least 3 characters.instead of default Please enter at least 3 characters.

字段 PostCode 必须至少包含 3 个字符。而不是默认请输入至少 3 个字符。

I need to override the default behaviour of this plugin, because I do not want to specify custom message for each item for every type of validation.

我需要覆盖此插件的默认行为,因为我不想为每种类型的验证为每个项目指定自定义消息。

Is it possible?

是否可以?

回答by Shef

$("#signupform")
    .validate({
        rules: {
            password: "required",
            postcode: {
                required: true,
                minlength: 3
            }
        },
        messages: {
            password: "Field Password is required",
            postcode: {
                required: "Field PostCode is required",
                minlength: "Field PostCode must contain at least 3 characters" 
            }
    });

回答by user828267

This is what I found when I was looking for the same thing a couple of days ago. Unfortunately I can't remember where I found it, so I can't give the person who wrote the answer credit.

这是我几天前在寻找同样的东西时发现的。不幸的是,我不记得我在哪里找到它,所以我不能给写答案的人功劳。

jQuery.extend(jQuery.validator.messages, {
    required:"This field is required.",
    equalTo:"Password fields do not match.",
    email:"Please enter a valid email.",
});

回答by detale

There's a more concise way to achieve this.

有一种更简洁的方法可以实现这一点。

Use "data-msg" or "data-msg-(rulename)" to override default error message for a predefined rule or a custom rule. (after jQuery validation v1.11.0, currently v1.16.0)

使用“ data-msg”或“ data-msg-(rulename)”来覆盖预定义规则或自定义规则的默认错误消息。(在 jQuery 验证 v1.11.0 之后,目前是 v1.16.0)

<input id="pwd" name="pwd" type="password" value=""
       required
       data-msg-required="Field Password is required." />

<input id="postcode" name="postcode" type="text" value=""
       required
       data-msg-required="Field PostCode is required."
       minlength="3"
       data-msg-minlength="Field PostCode must contain at least 3 characters." />

回答by Bill Keller

I needed to do the same thing, and ended up creating a custom method so that its message could be shared across multiple forms.

我需要做同样的事情,并最终创建了一个自定义方法,以便它的消息可以在多个表单之间共享。

I found this answer in a similar Stack Overflow question: How can we specify rules for jquery validation plugin by class?

我在一个类似的 Stack Overflow 问题中找到了这个答案:我们如何按类指定 jquery 验证插件的规则?

$.validator.addMethod(
    "newEmail", //name of a virtual validator
    $.validator.methods.email, //use the actual email validator
    "Custom error message"
);

回答by Bojan Srbinoski

add this after the plugin:

在插件后添加:

jQuery.extend(jQuery.validator.messages, {
    required: "Your new default message",
    email: "That's not a valid email"
});

you can put it in a separate file if you want, but make sure it's included after the plugin itself

如果你愿意,你可以把它放在一个单独的文件中,但要确保它包含在插件本身之后