How to specify a min but no max decimal using the range data annotation attribute?
Solution 1
It seems there's no choice but to put in the max value manually. I was hoping there was some type of overload where you didn't need to specify one.
[Range(typeof(decimal), "0", "79228162514264337593543950335")]
public decimal Price { get; set; }
Solution 2
How about something like this:
[Range(0.0, Double.MaxValue, ErrorMessage = "The field {0} must be greater than {1}.")]
That should do what you are looking for and you can avoid using strings.
Solution 3
If you are concerned about the string looking nice you could do this:
[Range(0, Double.PositiveInfinity)]
This will have a default error message of:
The field SuchAndSuch must be between 0 and Infinity.
Solution 4
You can use:
[Min(0)]
This will impose a required minimum value of 0 (zero), and no maximum value.
You need DataAnnotationsExtensions to use this.
Solution 5
If you're working with prices, I'm sure you can safely assume nothing will cost more than 1 trillion dollars.
I'd use:
[Range(0.0, 1000000000000)]
Or if you really need it, just paste in the value of Decimal.MaxValue
(without the commas): 79,228,162,514,264,337,593,543,950,335
Either one of these will work well if you're not from Zimbabwe.
Related videos on Youtube
user169867
Updated on May 20, 2020Comments
-
user169867 almost 4 years
I would like to specify that a decimal field for a price must be >= 0 but I don't really want to impose a max value.
Here's what I have so far...I'm not sure what the correct way to do this is.
[Range(typeof(decimal), "0", "??"] public decimal Price { get; set; }
-
Coops about 11 yearsSurely if this is going into a database you would need to specify the maximum allowed number based upon the selected database type? Otherwise you will get a nasty exception if this number is exceeded
-
-
user169867 almost 14 yearsThe problem is that is not a constant. You'll get this error: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
-
Bernie White about 12 yearsNo I don't think this is correct. It is not part of the standard MVC3 framework it is from Data Annotations Extensions dataannotationsextensions.org. Please provide a MSDN link.
-
Coops about 11 yearsWhy not just
[Range(0.0,Decimal.MaxValue)]
? -
Bronek almost 11 yearsI've used it for Int32 (Int32.MaxValue) and it is ok, thanks!
-
John Farrell almost 11 yearsWon't compile, Decimal.MaxValue isn't a constant.
-
Coops almost 11 yearsThat constant is a nuisance, referring to a resource file for error text is no easier
-
Piotr Kula almost 11 yearsIt does show a stupid validation message though :(
The field Fixed price discount must be between 0.01 and 1.79769313486232E+308.
-
Piotr Kula almost 11 yearsNO - definitely NOT part of MVC 3 :( BUt that library is a good extension to have any way :)
-
Piotr Kula almost 11 yearsThis code just look awful. I would suggest using dataannotationsextensions.org via nuget and as @Nicolai Schlenzig answered. Use
[Min(0)]
- This also has a better validation message. I would suggest updating your answer -
flafffl over 10 years@ppumkin Använd ErrorMessage , i.e. [Range(0.0, Double.MaxValue, ErrorMessage = "your error here")]
-
gentiane over 10 yearsNot part of MVC3 but it is not important. If you want validation on client side, you just need to use DataAnnotationsExtensions.MVC3 package. These two packages are available on nuget. I think that it is the best approach, as you don't have a stupid error message or don't need to redefine the error message each time you want to validate positive integer or decimal (which is fairly common).
-
pim about 9 yearsThanks Jacob. Great answer!
-
Fred almost 9 yearsNow you're making the assumption that the currency is dollar, not Yen or something else.
-
Vitani about 8 yearsThis is the best answer here IMHO, no extensions, no seemingly random strings/number, no custom code, and a reasonably sensible error message.
-
Alexander about 8 years@ppumkin inherit from DataAnnotationsModelValidator class to customize error messages
-
David T. Macknet over 7 yearsAs I pointed out below, but apparently it wasn't appreciated by someone.
-
Worthy7 over 7 yearsI updated it to make it the same as the best answer here, since the OP isn't changing his mind lol
-
Anastasios Selmani over 6 yearsThe answers above (@Jordan and @Jacob) are much more appropriate. Especially since we are talking about Price. I understand that many times transactions have to be done with decimal values but there isn't any price 1.234 dollars or at least most of the times you don't want to show this to the user.
-
Ε Г И І И О about 6 years@jfar Decimal.MaxValue IS a constant. It's just that the Range has no overload to accommodate a decimal.
-
Ε Г И І И О about 6 yearsYour error message should say "greater than or equal to".
-
David T. Macknet about 6 yearsGood catch. Added.
-
RoLYroLLs about 6 years@AnastasiosSelmanis, I agree with you, expect the part when you say "but there isn't any price 1.234 dollars". You are assuming USD, and even then, when you use this for foreign exchange (though not mentioned here by OP), USD does go into more decimals. =)
-
Enrico over 2 yearsWow that's cool. Are there any other advantages over using PositiveInfinity instead of maxvalue?
-
Auspex over 2 years@Enrico The meaningful message looks like the big advantage. Are there _dis_advantages? I wonder what happens if you enter a value too large for your database and less than infinity? In practice, I doubt I'll ever worry about it.