Skip to main content

Posts

Showing posts from December, 2010

Data Validation with Non-Inclusive Range Validator

The other day I was doing some validation for a few of my class's numeric properties. The valid range, I was told, was 0 to infinity. That's easy enough, I told myself. I'll just put a [Range] attribute each property and let the framework do its thing, like so...

[Range(0,double.MaxValue,ErrorMessage="Please specify a value greater than or equal to zero.")]
After publishing the web site to the staging server, I got an email from the client saying that 0 was not a valid value for some of the properties. My first thought was simply to change my range attribute this way:

Range(0.00000000001,double.MaxValue,ErrorMessage="Please specify a value greater than or equal to zero.")]
After all, who's going to type that many zeros? I still wasn't happy with that solution, so I started considering my alternatives. IDataErrorInfo was a definite option, and that would look this:

public string this[string propertyName] { get { switch (propertyName) …