MVC 5数据注释“不等于零”

时间:2018-07-21 14:18:15

标签: c# asp.net-mvc data-annotations

可能我缺少了一些东西,但是下面有模型

 public class MyModel
 {
     public double WhateverButNotZero { get; set; }
 }

是否有任何MVC内置的DataAnnotation来验证数字为“除零以外的所有内容”?

3 个答案:

答案 0 :(得分:3)

Regex to the rescue:

localhost/laravel/public/my_page

答案 1 :(得分:2)

尝试使用regex annotation

public class MyModel
{
    [RegularExpression("^(?!0*(\.0+)?$)(\d+|\d*\.\d+)$", ErrorMessage = "Not Equal to Zero")]
    public double WhateverButNotZero { get; set; }
}

答案 2 :(得分:0)

您可以使用RegularExpression DataAnnotation属性。

[RegularExpression(@"^\d*[1-9]\d*$")]
public double WhateverButNotZero { get; set; }

希望What is the regex for “Any positive integer, excluding 0”有助于根据您的需要找出正则表达式。