与asp.net MVC 3本地化jQuery验证 [英] Localizing jquery validation with asp.net mvc 3
问题描述
我使用Asp.Net MVC3和不显眼的jQuery验证。我想有我的日期验证本地化的,我的意思是,jQuery是验证我日期是MM / DD / YYYY,但我想它被DD / MM / YYYY。
我试图使用jQuery的插件全球化时代( http://github.com/jquery/globalize )。
我加入脚本globalize.js和globalize.culture.pt-BR.js当我的页面加载我运行follwing脚本引用:
(函数(){
$(函数(){
$ .datepicker.setDefaults($ datepicker.regional ['PT-BR']);
Globalize.culture(PT-BR);
});
})调用(本);
在jQuery UI的插件工作原理的魅力,但验证没有。
还有什么我缺少什么?
编辑:
在回答使用下面的链接我使用全球化插件解决了这个问题:
当然,我不得不添加一个引用页面全球化时代的插件,也到了我想用文化的引用(所有可用的插件的网站)。之后,这只是一小部分的JavaScript code的。
Globalize.culture(PT-BR);
$ .validator.methods.date =功能(价值元素){
返回this.optional(元)|| Globalize.parseDate(值);
};
如果你正在做的与国际化和ASP.NET MVC我强烈建议通过纳迪姆Afana
这两个优秀的帖子读取任何工作在他的第二个职位他已经使用jQuery UI日期选择器的详细示例和本地化讨论的问题。
在他的例子,他提到了以下
@ *不幸的是,日期选择器只支持中性文化,所以我们需要调整日期和时间格式的特定文化* @
$(#EVENTDATE)。改变(函数(){
$(本).VAL(Globalize.format($(本).datetimepicker('GETDATE'),Globalize.culture()。calendar.patterns.d ++ Globalize.culture()。calendar.patterns.t) ); / * D T * /
});
我还建议下载他的网站上链接的书呆子晚餐国际化演示。
I am using Asp.Net Mvc3 and the unobtrusive jquery validation. I'd like to have my dates validation localized, I mean, jquery is validating my date as being MM/dd/yyyy but I would like it to be dd/MM/yyyy.
I'm trying to use the jQuery Globalize plugin (http://github.com/jquery/globalize). I added references to the scripts globalize.js and globalize.culture.pt-BR.js and when my page loads I'm running the follwing script:
(function() {
$(function() {
$.datepicker.setDefaults($.datepicker.regional['pt-BR']);
Globalize.culture("pt-BR");
});
}).call(this);
The jQuery UI plugin works as charm, but the validation doesn't. What else am I missing?
Edit:
Using the links in the answer below I solved the problem using the Globalize plugin:
Of course, I had to add a reference to the Globalize plugin in the page and also a reference to the culture that I wanted to use (all available on the plugin's site). After that is just a small piece of JavaScript code.
Globalize.culture("pt-BR");
$.validator.methods.date = function(value, element) {
return this.optional(element) || Globalize.parseDate(value);
};
If you are doing any work with internationalization and ASP.NET MVC I highly recommend reading through these two excellent posts by Nadeem Afana
In his second post he has a detailed example of using the jQuery UI datepicker and discusses the issues with localization.
In his example he mentions the following
@* Unfortunately, the datepicker only supports Neutral cultures, so we need to adjust date and time format to the specific culture *@
$("#EventDate").change(function(){
$(this).val(Globalize.format($(this).datetimepicker('getDate'), Globalize.culture().calendar.patterns.d + " " + Globalize.culture().calendar.patterns.t)); /*d t*/
});
i also recommend downloading the Nerd Dinner internationalization demo linked on his site.
这篇关于与asp.net MVC 3本地化jQuery验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!