如何更改JQuery Validator语言消息

我正在使用http://bassistance.de/jquery-plugins/jquery-plugin-validation/中的JQuery Validator。 我怎样才能使消息是自定义的而不是英文的。

像这样做:

$(document).ready(function() { $("form#login").validate({ lang: 'en' // or whatever language option you have. }); }); 

如果您要提供的语言不是默认语言之一,请执行以下操作:

 $.tools.validator.localize("fi", { '*' : 'Virheellinen arvo', ':email' : 'Virheellinen sähköpostiosoite', ':number' : 'Arvon on oltava numeerinen', ':url' : 'Virheellinen URL', '[max]' : 'Arvon on oltava pienempi, kuin $1', '[min]' : 'Arvon on oltava suurempi, kuin $1', '[required]' : 'Kentän arvo on annettava' }); $("form#login").validate({ lang: 'fi' }); 

请参阅这些说明了解更多信

如果您查看目录“本地化”,您可以找到不同的.js文件,这些文件包含不同语言的错误消息。 [类似“messages_XX.js”]

在包含jquery.validate.js之后,选择所需语言的文件并将以下行添加到标记中

  

最好的方法是在需要时扩展插件

 $.extend($.validator.messages, { required: "my required message", .... }); 

这将是你的初始validation脚本中的JSON结构,就像Alex有 :

  rules: { accntTypeCL: { required: true, }, accntNoCL: { required: true, minlength: 19, numberDash: true, } }, messages : { accntTypeCL : { required : Messages.ERR_TEST, }, accntNoCL : { required : Messages.ERR_TEST, numberDash : Messages.ERR_TEST, minlength : Messages.ERR_TEST2, }, } //This would be in your messages.js file... But you'll need to make sure you are using a Java backend or something that will pull the messages.js correctly //For IBM worklight this worked great Messages = { // Add here your messages for the default language. // Generate a similar file with a language suffix containing the translated messages ERR_TOPLEVEL : 'One or more of the required fields was left blank or is invalid.<\/span>', //Test Messages for tracing ERR_TEST: 'This be the test yar!', ERR_TEST2: 'This be the test2 yar!' }; 

通过这种方式,您可以重复使用相同的函数,相同的附加方法和相同的错误类型,并根据应在浏览器中检测到的html语言使用正确的messages.js文件,或者您拥有它。 这种特殊方法对我有用。

看看我的解决方案

 jQuery.extend(jQuery.validator.messages, { required: abp.localization.localize("FormValidationMessageRequired"),//"This field is required.", remote: "Please fix this field.", email: abp.localization.localize("FormValidationMessageEmail"),//"Please enter a valid email address.", url: abp.localization.localize("FormValidationMessageUrl"),//"Please enter a valid URL.", date: abp.localization.localize("FormValidationMessageDate"),//"Please enter a valid date.", dateISO: "Please enter a valid date (ISO).", number: abp.localization.localize("FormValidationMessageNumber"),//"Please enter a valid number.", digits: "Please enter only digits.", creditcard: "Please enter a valid credit card number.", equalTo: abp.localization.localize("FormValidationMessageDataEquals"),//"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(abp.localization.localize("FormValidationMessageMinlength")),//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(abp.localization.localize("FormValidationMessageMax")),//jQuery.validator.format("Please enter a value less than or equal to {0}."), min: jQuery.validator.format(abp.localization.localize("FormValidationMessageMin"))//jQuery.validator.format("Please enter a value greater than or equal to {0}.") }); 

而这个函数abp.localization.localize(Key)返回基于当前文化的本地化字符串,这个函数来自我用的框架,叫做aspnetboilerplate

有关更多信息,请参阅此堆栈溢出线程jQueryvalidation:更改默认错误消息

使用messages对象。

定义自定义消息的键/值对。 Key是元素的名称,它为要为该元素显示的消息赋值。 可以使用具有针对每个规则的特定消息的另一个映射而不是简单消息。 覆盖元素的title属性或方法的默认消息(按此顺序)。 每条消息都可以是String或Callback。 回调函数在validation程序的范围内调用,并且规则的参数作为第一个参数,元素作为第二个参数,它必须返回一个String作为消息显示。

 $(".selector").validate({ rules: { name: "required", email: { required: true, email: true } }, messages: { name: "Please specify your name", email: { required: "We need your email address to contact you", email: "Your email address must be in the format of name@domain.com" } } }) 

来源 。

您也可以将错误消息直接放在标记中,如下所示:

   

见文档

如果您使用某种本地化插件,则可以将消息移出单独的文件中。 这里我使用i18n-2(npm模块):

  

然后我将我的语言文件放在一个文件夹中:

 /locales da.json en.json 

en.json

 "pages": { "apply": { "subtitle": "Apply here", "form": { "email": { "title": "Email", "placeholder": "Your email address", "warning": "NB! DER AFSENDES EN MAIL HERTIL", "errormsg": { "required": "Enter a valid email address" } } } } } 

只需在定义validation的json中输入“required”值。 检查演示源,但它在消息类别中

游戏的后期,但如果您使用相同的模板进行多种语言,则可以内联:

 if($('html').attr('lang')=='he'){ $('form').validate({ messages: { email: "חובה", phone: "חובה", zip: "חובה" } }); }else{ $('form').validate({ messages: { email: "Required", phone: "Required", zip: "Required" } }); };