5

I'm decorated a ViewModel in my ASP.NET MVC 2 site with System.ComponentModel.DataAnnotations validation attributes. For one of my fields, named Price, I want to validate that the value is not below some extent, in this case 0.

I know that RangeAttribute exists for validation with a lower and an upper extent, but does something like a MinimumValueAttribute exist for validation with only a lower extent?

If not, I guess I'll just roll my own.

Maxim Zaslavsky
  • 17,149
  • 30
  • 100
  • 167
  • remember that rolling your own is easy for server side, but you have to rewrite the validation for client side. – Chase Florell Jul 04 '10 at 22:54
  • @rockinthesixstring that was why I asked the question rather than just going ahead and implementing my own. of course, you can use Reflector to see how `RangeAttribute` is doing client-side validation. :) – Maxim Zaslavsky Jul 04 '10 at 22:56
  • yup... I just felt it needed to be said for the next guy that comes along to this question. Rolling your own validation is a pain in the ass, especially if you have to reuse the validation on multiple views. – Chase Florell Jul 04 '10 at 22:59
  • what would be nice is if "somehow" the MVC framework could read through the serverside validation and build custom validators for the client. I suppose another option would be to crack open the source in the HTML Helper Class of MVC and add your custom client side validation in there as well as in the `MicrosoftMvcValidation.js`, and then recompile it specifically for your app. – Chase Florell Jul 04 '10 at 23:01
  • you can always use server side validation via an ajax call. Bypass the need to duplicate all the code & craft on both sides. – redsquare Jul 04 '10 at 23:40

2 Answers2

10

before you roll your own - try [Range(0.05, Double.MaxValue)]

Felix
  • 5,814
  • 8
  • 44
  • 75
2

Not sure if RegularExpression is the "best" solution, but you can give it a shot

"^\$?\d+(\.(\d{2}))?$"
Chase Florell
  • 42,985
  • 56
  • 169
  • 364