简体   繁体   English

如何验证 Asp.net 中的 integer 是否属于具有内置验证的多个间隔(或有异常的范围)?

[英]How to validate an integer in Asp.net to fall into multiple intervals with built-in validation (or a range with exception)?

I have an integer that can fall in range from -1000 to 1000 but cannot be zero.我有一个 integer 可以在 -1000 到 1000 的范围内但不能为零。 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.理想情况下不是正则表达式,尽管我想这将是最简单的方法。

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但是您可以使用FluentValidation轻松实现编写验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM