Reputation: 25260
In my ViewModel, I have the following System.ComponentModel.DataAnnotations
on a property that contains USD currency:
[DisplayFormat(DataFormatString = "{0:C2}")]
[Range(0.01, 100000, ErrorMessage = "Payment amount is required between .01 and $100,000.")]
[DataType(DataType.Currency)]
[DisplayName("Payment Amount")]
public Double PrinAmount { get; set; } = 0.00;
When I enter a value of $10.005
, I get the following validation model error from the ModelState.IsValid
check:
The value '$10.005' is not valid for Payment Amount.
When I enter a value 10.005
, the ModelState.IsValid
is equal to true
.
What do I need to do to modified the validation to capture both formats as invalid?
Upvotes: 0
Views: 2680
Reputation: 4344
You can use Regular Expression;
[RegularExpression(@"^\d+\.\d{0,2}$")]
the DataAnnotations above ensures 2 digits.
Upvotes: 1