How to specify a min but no max decimal using the range data annotation attribute?

197,266

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.

Share:
197,266

Related videos on Youtube

user169867
Author by

user169867

Updated on May 20, 2020

Comments

  • user169867
    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
      Coops about 11 years
      Surely 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
    user169867 almost 14 years
    The 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
    Bernie White about 12 years
    No 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
    Coops about 11 years
    Why not just [Range(0.0,Decimal.MaxValue)]?
  • Bronek
    Bronek almost 11 years
    I've used it for Int32 (Int32.MaxValue) and it is ok, thanks!
  • John Farrell
    John Farrell almost 11 years
    Won't compile, Decimal.MaxValue isn't a constant.
  • Coops
    Coops almost 11 years
    That constant is a nuisance, referring to a resource file for error text is no easier
  • Piotr Kula
    Piotr Kula almost 11 years
    It does show a stupid validation message though :( The field Fixed price discount must be between 0.01 and 1.79769313486232E+308.
  • Piotr Kula
    Piotr Kula almost 11 years
    NO - definitely NOT part of MVC 3 :( BUt that library is a good extension to have any way :)
  • Piotr Kula
    Piotr Kula almost 11 years
    This 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
    flafffl over 10 years
    @ppumkin Använd ErrorMessage , i.e. [Range(0.0, Double.MaxValue, ErrorMessage = "your error here")]
  • gentiane
    gentiane over 10 years
    Not 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
    pim about 9 years
    Thanks Jacob. Great answer!
  • Fred
    Fred almost 9 years
    Now you're making the assumption that the currency is dollar, not Yen or something else.
  • Vitani
    Vitani about 8 years
    This is the best answer here IMHO, no extensions, no seemingly random strings/number, no custom code, and a reasonably sensible error message.
  • Alexander
    Alexander about 8 years
    @ppumkin inherit from DataAnnotationsModelValidator class to customize error messages
  • David T. Macknet
    David T. Macknet over 7 years
    As I pointed out below, but apparently it wasn't appreciated by someone.
  • Worthy7
    Worthy7 over 7 years
    I updated it to make it the same as the best answer here, since the OP isn't changing his mind lol
  • Anastasios Selmani
    Anastasios Selmani over 6 years
    The 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 years
    Your error message should say "greater than or equal to".
  • David T. Macknet
    David T. Macknet about 6 years
    Good catch. Added.
  • RoLYroLLs
    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
    Enrico over 2 years
    Wow that's cool. Are there any other advantages over using PositiveInfinity instead of maxvalue?
  • Auspex
    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.