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

C#.Netasp.net MvcData Annotations

C# Problem Overview


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; }

C# Solutions


Solution 1 - C#

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 2 - C#

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 3 - C#

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 4 - C#

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 - C#

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.

Solution 6 - C#

You can use custom validation:

    [CustomValidation(typeof(ValidationMethods), "ValidateGreaterOrEqualToZero")]
    public int IntValue { get; set; }

    [CustomValidation(typeof(ValidationMethods), "ValidateGreaterOrEqualToZero")]
    public decimal DecValue { get; set; }

Validation methods type:

public class ValidationMethods
{
    public static ValidationResult ValidateGreaterOrEqualToZero(decimal value, ValidationContext context)
    {
        bool isValid = true;

        if (value < decimal.Zero)
        {
            isValid = false;
        }

        if (isValid)
        {
            return ValidationResult.Success;
        }
        else
        {
            return new ValidationResult(
                string.Format("The field {0} must be greater than or equal to 0.", context.MemberName),
                new List<string>() { context.MemberName });
        }
    }
}

Solution 7 - C#

I was going to try something like this:

[Range(typeof(decimal), ((double)0).ToString(), ((double)decimal.MaxValue).ToString(), ErrorMessage = "Amount must be greater than or equal to zero.")]

The problem with doing this, though, is that the compiler wants a constant expression, which disallows ((double)0).ToString(). The compiler will take

[Range(0d, (double)decimal.MaxValue, ErrorMessage = "Amount must be greater than zero.")]

Solution 8 - C#

using Range with

[Range(typeof(Decimal), "0", "9999", ErrorMessage = "{0} must be a decimal/number between {1} and {2}.")]

[Range(typeof(Decimal),"0.0", "1000000000000000000"]

Hope it will help

Solution 9 - C#

[Range(0.01,100000000,ErrorMessage = "Price must be greter than zero !")]

Solution 10 - C#

I would put decimal.MaxValue.ToString() since this is the effective ceiling for the decmial type it is equivalent to not having an upper bound.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser169867View Question on Stackoverflow
Solution 1 - C#JacobView Answer on Stackoverflow
Solution 2 - C#JordanView Answer on Stackoverflow
Solution 3 - C#user169867View Answer on Stackoverflow
Solution 4 - C#Nicolai SchlenzigView Answer on Stackoverflow
Solution 5 - C#John FarrellView Answer on Stackoverflow
Solution 6 - C#Erikas PliaukstaView Answer on Stackoverflow
Solution 7 - C#David T. MacknetView Answer on Stackoverflow
Solution 8 - C#AbiView Answer on Stackoverflow
Solution 9 - C#Sohail AkhterView Answer on Stackoverflow
Solution 10 - C#Dr HerbieView Answer on Stackoverflow