【问题标题】:MVC DataAnnotations should accept decimal without zeroMVC DataAnnotations 应该接受不带零的十进制
【发布时间】:2019-09-23 14:01:29
【问题描述】:

下面是我的数据注释

[RegularExpression(@"^((\d+)((\.\d{1,4})?))$", ErrorMessage = "PartSize must be numeric / 4 digit decimal")]

它接受 0.52 但拒绝 0.52!

可以向该验证器添加什么?

【问题讨论】:

    标签: regex model-view-controller data-annotations


    【解决方案1】:

    这个正则表达式应该可以解决问题:

    (?=[\d\.])(?'number'\d*(\.\d{1,4})?)
    

    测试here

    主要技巧是我将\d+ 更改为\d* 以捕获第一个数字(允许缺少整数部分)。

    我添加了前瞻来删除零长度匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-05
      • 1970-01-01
      • 2014-04-30
      相关资源
      最近更新 更多