2013-02-18 189 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位小數。