如何使用数据注释验证验证给定数量不大于某个数量?
How to validate that a given amount is not greater than some amount using Data annotation validations?
我有一个class
Public class Transaction{
[Required]
//[RegularExpression(@"\d{1, 5}\.\d{1,2}", ErrorMessage = "Amount has to be
positive and upto 2 decimal places")]
[Range(0, 20000, ErrorMessage = "Maximum transaction amount can not exceed
20000.")]
[RegularExpression(@"^[0-9]*(\.[0-9]{1,2})?$", ErrorMessage = "Amount has to
be positive and upto 2 decimal places")]
public float Amount { get; set; }
}
我不希望用户输入大于 20K 的金额,并且在任何时候都只能保留两位小数。
我已经写了上面的验证,但是当我输入一个大数字时,比如 1453668789564565656。看起来范围验证抛出异常 "value was either too large or too small for an int32."
有人可以在这里指导我吗?我怎样才能完成这项工作?
您需要使用带双参数的构造函数,即
[Range(0.0, 20000.0, ErrorMessage = "Maximum transaction amount can not exceed 20000.")]
我有一个class
Public class Transaction{
[Required]
//[RegularExpression(@"\d{1, 5}\.\d{1,2}", ErrorMessage = "Amount has to be
positive and upto 2 decimal places")]
[Range(0, 20000, ErrorMessage = "Maximum transaction amount can not exceed
20000.")]
[RegularExpression(@"^[0-9]*(\.[0-9]{1,2})?$", ErrorMessage = "Amount has to
be positive and upto 2 decimal places")]
public float Amount { get; set; }
}
我不希望用户输入大于 20K 的金额,并且在任何时候都只能保留两位小数。
我已经写了上面的验证,但是当我输入一个大数字时,比如 1453668789564565656。看起来范围验证抛出异常 "value was either too large or too small for an int32."
有人可以在这里指导我吗?我怎样才能完成这项工作?
您需要使用带双参数的构造函数,即
[Range(0.0, 20000.0, ErrorMessage = "Maximum transaction amount can not exceed 20000.")]