问题描述
|
我正在使用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,\'[required]\' : \'Kentän arvo on annettava\'
});
$(\"form#login\").validate({
lang: \'fi\'
});
有关更多信息,请参见这些说明。
, 如果查看目录\“ localization \”,您可能会发现不同的.js文件,其中包含不同语言的错误消息。
[类似于\“ messages_XX.js \”]
选择所需语言的文件,然后在包含jquery.validate.js之后,将以下行添加到标记中
<script type=\"text/javascript\" src=\"localization/messages_XX.js\"></script>
, 最好的方法是在需要时扩展插件
$.extend($.validator.messages,{
required: \"my required message\",....
});
, 这是像Alex一样的初始验证脚本中的JSON结构:
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 : \'<span role=\\\"presentation\\\">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文件,或者您可以使用它。这种特殊的方法对我很有用。
, 您还可以将错误消息直接放入标记中,如下所示:
<input required data-msg=\"Please fill this field\">
<input data-rule-minlength=\"2\" data-rule-maxlength=\"4\" data-msg-minlength=\"At least two chars\" data-msg-maxlength=\"At most fours chars\">
参阅文件
如果使用某种本地化插件,则可以将消息移到单独的文件中。在这里,我使用i18n-2(npm模块):
<input id=\"email\" type=\"email\" name=\"email\" data-msg=__(\"pages.apply.form.email.errormsg.required\"))
然后我将语言文件放在一个文件夹中:
/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\"
}
}
}
}
}
, 看看我的解决方案
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}.\")
});
并且此funcabp.localization.localize(Key)
返回基于当前区域性的本地化字符串,此函数来自我使用的称为aspnetboilerplate的框架
有关更多信息,请参见此堆栈溢出线程jQuery验证:更改默认错误消息
, 使用ѭ11对象。
定义自定义的键/值对
消息。键是名称
元素,重视要显示的消息
对于那个元素。而不是平原
向其他地图发送具体消息
可以使用每个规则的消息。
覆盖的标题属性
元素或默认消息
方法(按此顺序)。每条讯息
可以是字符串或回调。的
回调在
验证者和规则的
参数作为第一个和
元素作为第二种装饰,它
必须返回一个字符串以显示为
信息。
例
$(\".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\"
}
}
})
资源。
, 如果您使用npm
/yarn
管理资产,则可以像这样简单地导入本地化文件(当然,请替换iso代码,这里是法语):
import \'jquery-validation\';
import \'jquery-validation/dist/localization/messages_fr\';
然后使用:
$form.validate({
lang: \'fr\',});
, 只需在定义验证的json中输入一个\“ required \”值即可。检查演示源,但在消息类别中
, 游戏晚了,但是如果您将同一模板用于多种语言,则可以内联:
if($(\'html\').attr(\'lang\')==\'he\'){
$(\'form\').validate({
messages: {
email: \"חובה\",phone: \"חובה\",zip: \"חובה\"
}
});
}else{
$(\'form\').validate({
messages: {
email: \"Required\",phone: \"Required\",zip: \"Required\"
}
});
};