使用 asp.net mvc 3 本地化 jquery 验证
我正在使用 Asp.Net Mvc3 和不显眼的 jquery 验证。我想让我的日期验证本地化,我的意思是,jquery 正在验证我的日期为 MM/dd/yyyy,但我希望它是 dd/MM/yyyy。
我正在尝试使用 jQuery Globalize 插件 (http://github.com/jquery/globalize)。 我添加了对脚本 globalize.js 和 globalize.culture.pt-BR.js 的引用,当我的页面加载时,我正在运行以下脚本:
(function() {
$(function() {
$.datepicker.setDefaults($.datepicker.regional['pt-BR']);
Globalize.culture("pt-BR");
});
}).call(this);
jQuery UI 插件可以正常工作,但验证却不能。 我还缺少什么?
编辑:
使用下面答案中的链接,我使用全球化插件解决了问题:
当然,我必须在页面中添加对 Globalize 插件的引用,以及对我想要使用的文化的引用(所有这些都可以在插件的网站上找到)。之后就是一小段 JavaScript 代码。
Globalize.culture("pt-BR");
$.validator.methods.date = function(value, element) {
return this.optional(element) || Globalize.parseDate(value);
};
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);
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我自己最近也在做类似的事情。我首先遵循 Scott Hanselman 博客中关于此主题的建议 - 您可以在此处阅读相关内容:
http://www.hanselman .com/blog/GlobalizationInternationalizationAndLocalizationInASPNETMVC3JavaScriptAndJQueryPart1.aspx
我必须进行一些更改才能使用 Globalize 而不是jQuery Global(现在 jQuery Global 已死)。我在以下博客文章中写了这一点,以防有帮助:
http:// icanmakethiswork.blogspot.co.uk/2012/09/globalize-and-jquery-validate.html
我的博客文章提供了一个指向此脚本 jquery.validate.globalize.js 的链接,该脚本强制使用 jQuery验证是否使用 Globalize 进行数字/日期/范围解析。其中的日期部分可能应该解决您的问题:
https://raw.github.com/gist/3651751/68cbccd0fdd4725a8d6fd1b5568bb33d27fb1eff/jquery.validate.globalize.js
I've been doing similar myself recently. I started by following the advice in Scott Hanselman's blog on this topic - you can read about this here:
http://www.hanselman.com/blog/GlobalizationInternationalizationAndLocalizationInASPNETMVC3JavaScriptAndJQueryPart1.aspx
I had to make some changes to use Globalize instead of jQuery Global (now jQuery Global is dead). I wrote this up in the following blog post in case that's helpful:
http://icanmakethiswork.blogspot.co.uk/2012/09/globalize-and-jquery-validate.html
My blog post features a link to this script jquery.validate.globalize.js which forces jQuery Validate to use Globalize for number / date / range parsing. The date part of this is the part that should probably solve your issue:
https://raw.github.com/gist/3651751/68cbccd0fdd4725a8d6fd1b5568bb33d27fb1eff/jquery.validate.globalize.js
如果您正在从事国际化和 ASP.NET MVC 方面的工作,我强烈建议您阅读 Nadeem Afana 的这两篇优秀文章
在他的第二篇文章中,他提供了使用 jQuery UI 日期选择器的详细示例,并讨论了本地化问题。
在他的示例中,他提到了以下内容,
我还建议下载他网站上链接的 Nerddinner 国际化演示。
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
i also recommend downloading the Nerd Dinner internationalization demo linked on his site.
对 Johnny Reilly 答案的一点修正:
必须替换
为正确解析零字符串(“0”)。
所以整个代码是:
Little correction of Johnny Reilly answer:
must be replaced with
for correct parsing of zero string ("0").
So entire code is: