1

I have an integer that can fall in range from -1000 to 1000 but cannot be zero. I cannot find any built-in validation that can fix this:

  • Range can be only used once and does not allow exceptions or intervals
  • Required does not allow zero validation

I know I can just write a custom attribute, but was wondering if I can achieve this in a simple way that I cannot find.

And ideally not regular expression, though I guess that would be the easiest way.

Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207

1 Answers1

0

You cannot exclude the value from a range. Using ranges is as simple as that (but it DOESN'T support any excluding):

[Range(-1000, 1000)]
public int Amount {get; set;}

But you can easily achieve it writing validation using FluentValidation

Morasiu
  • 1,204
  • 2
  • 16
  • 38