Best Data annotation for a Decimal(18,2)

asp.net-mvc c# entity-framework

Question

I have a column inside my sql server 2008 wih type of Decimal(18,2). But on entity framework what is the best data annotation validation I can apply to this property, inside my asp.net MVC web application ?

1
37
10/1/2018 11:04:29 AM

Popular Answer

There is no explicit data annotation for a decimal so you need to use two separate ones to add constraints.

Two Decimal Points

[RegularExpression(@"^\d+\.\d{0,2}$")]

This regular expression will make sure that the property has at most two decimal places.

Max 18 digits

[Range(0, 9999999999999999.99)]

Assuming you aren't accepting any negative numbers. Otherwise, replace 0 with -9999999999999999.99.

Result

[RegularExpression(@"^\d+\.\d{0,2}$")]
[Range(0, 9999999999999999.99)]
public decimal Property { get; set; }
58
8/31/2016 8:12:36 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow