1

I have a product order page where the minimum order is 2500. I want to use the Range annotation validation in the model to validate this, but I also need the user to be able to select 0 of this product if they don't want any.

Now I use:

[Display(Name = "Item1")]
[Range(1000, int.MaxValue, ErrorMessage = "You need to order minimum {1} of Item1")]
public int OrderedItem1{ get; set; }

Is there an easy way to accomplish this without creating a custom validator?

4

2 回答 2

1

是的,您可以使用正则表达式验证属性。

[RegularExpression(@"SomeRegExpression", ErrorMessage = "Min order error")]
于 2012-05-21T16:26:34.940 回答
0

我发现我可以使用这个正则表达式验证属性来做到这一点:

[RegularExpression(@"^(?:0|\d{5,}|[1-9]\d\d\d)$", ErrorMessage = "You need to order minimum 1000 of Item1")]

感谢瑞安·约翰逊的帮助。

于 2012-05-22T08:38:22.803 回答