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; }
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.
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.
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; }
[Min(0)]
- This also has a better validation message. I would suggest updating your answer
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.
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.
[Range(0.0,Decimal.MaxValue)]
?
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 });
}
}
}
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.")]
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
[Range(0.01,100000000,ErrorMessage = "Price must be greter than zero !")]
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.
Success story sharing
The field Fixed price discount must be between 0.01 and 1.79769313486232E+308.