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?

2 Answers 2

1

Yes, you could use the regular expression validation attribute.

[RegularExpression(@"SomeRegExpression", ErrorMessage = "Min order error")]
Sign up to request clarification or add additional context in comments.

Comments

0

I found out that I could do this using this regular expression validation attribute:

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

Thanks to Ryand Johnson for helping out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.