MVC3 jQuery的不显眼的验证区域设置小数点场 [英] mvc3 jquery unobtrusive validation locale decimal field

查看:163
本文介绍了MVC3 jQuery的不显眼的验证区域设置小数点场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地化MVC3 Web应用程序使用不显眼的审定工作。
在web.config中我有:

I'm working on localized mvc3 web application using unobtrusive validation. In web.config I've got :

<globalization culture="pl-PL" uiCulture="pl" />

jQuery的1.4.4和jQuery验证1.6在使用中。

Jquery 1.4.4 and jquery validation 1.6 is in use.

问题是小数分隔符。

我可以看到,jQuery验证是忽略了文化,期待一个点字符永远是小数点分隔符。我需要使用逗号来代替。我认为这是在德语例如相同的。

I can see that jquery validation is ignoring the culture and is expecting a dot character to always be the decimal separator. I need to use comma instead. I think it's the same in German language for example.

我创建了自己methods_pl.js文件:

I created my own methods_pl.js file :

jQuery.extend(jQuery.validator.methods, {
number: function(value, element) {
    return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:\.\d{3})+)(?:,\d+)?$/.test(value);
}
});

以上是解决在哪里十进制数完全不认识的基本问题。

Above is solving the basic problem where decimal number is not recognized at all.

但是,当我尝试使用RangeAttribute在我的模型小数房地产价格仍然不起作用。
如何解决此问题?

But when I try to use RangeAttribute on my model Decimal Price property it still doesn't work. How to solve this?

推荐答案

如果您设置

key="UnobtrusiveJavaScriptEnabled" value="false"

DecimalModelBinder 公布在previous答案工作,但要禁用CLIEN端验证。

the DecimalModelBinder posted in the previous answer will work, but you are disabling the clien-side validations.

要解决你的问题,你需要inlcude正确的jQuery的语言环境。
看起来<一个href=\"http://www.hanselman.com/blog/GlobalizationInternationalizationAndLocalizationInASPNETMVC3JavaScriptAndJQueryPart1.aspx\"相对=nofollow>此帖来自斯科特Hanselmann 或的这个帖子他们应该给你在实施它的一些帮助。

To solve your problem you need to inlcude the correct jQuery locale. Look a this post from Scott Hanselmann or this post they should give you some help in implementing it.

您应该添加Jquery.globalize插件形式的NuGet然后在domready中函数中添加类似的东西,有一些正确验证(至少它为我的作品)

You should add the Jquery.globalize plugin form NuGet then in your DOMready function add something like that to have number validated correctly (at least it works for me)

 $.validator.methods.number = function (value, element) {
        if (Globalize.parseFloat(value)) 
            return true;
        return false;
    }
 Globalize.culture('fr-FR');

然后在页面中添加脚本相对

then add the relative scripts in your page

<script src="@Url.Content("~/Scripts/jquery-1.7.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.globalize/globalize.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.globalize/cultures/globalize.culture.fr-FR.js")" type="text/javascript"></script>

这篇关于MVC3 jQuery的不显眼的验证区域设置小数点场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆