我需要一个RegEx来检查商品订单数量是0还是介于2500和999999之间。这甚至可以吗?
实施例: 您可以订购0(无物品),也可以订购2500件或更多物品。
更新 这需要是一个RegEx,因为它将用于MVC中的验证属性。
[RegularExpression(@"SomeRegExpression", ErrorMessage = "Min order error")]
答案 0 :(得分:3)
如果必须是正则表达式:
^(?:0|\d{5,6}|2[5-9]\d\d|[3-9]\d\d\d)$
说明:
^ # Start of string
(?: # Either match...
0 # 0
| # or
\d{5,6} # a five- or six-digit number
| # or
2[5-9]\d\d # 2500-2999
| # or
[3-9]\d\d\d # 3000-9999
) # End of alternation
$ # End of string
答案 1 :(得分:3)
您也可以编写自己的自定义验证属性。有关示例,请参阅How to create custom validation attribute for MVC和/或http://www.codeproject.com/Articles/301022/Creating-Custom-Validation-Attribute-in-MVC-3。
例如;
public class CustomValidationAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
int number = value as int;
return (number == 0 || (number >= 2500 && number <= 999999));
}
}