2013-02-18 107 views
0

mvc4范围验证小数

[Range(typeof(decimal), "75000", "300000")] 
public decimal Importo { get; set; } 

[Range(75000, 300000)] 
public decimal Importo { get; set; } 

对数据库字段:decimal(10, 2)

,当我的页面加载,我得到75000,00中,如果我输入 提交表单我得到的错误

The field Importo must be between 75000 and 300000(我知道了loca在意大利语中使用globalize.js)

但是75000,00必须是有效值。

我阅读使用正则表达式。任何希望只使用范围属性?

+0

解决加入 [代码] <脚本类型= “文本/ JavaScript的”> jQuery.extend(jQuery.validator.methods,{ 范围:函数(值,元素,PARAM){ //使用全球化插件来解析值 VAR VAL = Globalize.parseFloat(值); 返回this.optional(元件)||( 值> =参数[0] && VAL <=参数[1]);} } ); [/ code] – Emanuele 2013-02-18 13:41:12

回答

0
+0

等解决方案?我试了一下,但它不满意我 – Emanuele 2013-02-18 13:17:39

+0

正如在这篇文章中所述,范围验证器不支持小数,所以它加倍或使用正则表达式。 http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.rangeattribute.aspx – Lanayx 2013-02-19 09:20:08

+0

我阅读文档,但我解决了。谢谢 – Emanuele 2013-09-12 08:45:10

1

尝试添加该

[RegularExpression(@"^\d+.\d{0,2}$",ErrorMessage = "Price must can't have more than 2decimal places")] 

此行可以接受2位小数。