【问题标题】:Can not find a [RegularExpression] inside my asp.net mvc to allow max of 2 digits在我的 asp.net mvc 中找不到 [RegularExpression] 以允许最多 2 位数字
【发布时间】:2016-09-12 21:54:24
【问题描述】:

我正在开发一个 asp.net mvc Web 应用程序,并且我在 sql server Decimal(19,2) 中有一个具有以下数据类型的十进制字段。现在我想检查用户只能输入 2 位数字,但他们可以添加数字,例如 10 、 20 (没有任何数字).. 但如果他们设置数字以检查最多两位数字。

现在我尝试了以下正则表达式,但没有一个效果很好:-

这个正则表达式不允许用户输入不包含数字的数字:-

[RegularExpression(@"^\d+.\d{0,2}$", ErrorMessage = "Value can't have more than 2 decimal places")]
public Nullable<decimal> CostPrice { get; set; }

如果用户尝试输入数字,此正则表达式将引发错误:-

[RegularExpression(@"^(\d{0,2})$", ErrorMessage = "error Message")]
public Nullable<decimal> CostPrice { get; set; }

那么任何人都可以推荐什么是最好的正则表达式,它强制用户输入最多 2 位数字,同时允许他们输入没有任何数字的数字?

【问题讨论】:

  • 数字是指小数位吗?
  • @DoNothing 是的,精确到小数位

标签: c# asp.net regex asp.net-mvc asp.net-mvc-4


【解决方案1】:

说明

^(?!\s*[0-9]{0,2}\s*$).*$

此正则表达式将执行以下操作:

  • 验证字符串最多为 2 位数字并被空格包围
  • 如果字符串最多包含 2 位数字,则表达式为假
  • 如果字符串包含超过 2 位数字,则表达式为真
  • 如果字符串包含任何字母,则表达式为真

现场示例

https://regex101.com/r/vQ1gW1/1

说明

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
    [0-9]{0,2}               any character of: '0' to '9' (between 0
                             and 2 times (matching the most amount
                             possible))
----------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------

【讨论】:

  • 但您的示例 > 与我正在搜索的问题无关for 最多接受 2 位小数,所以这些在我的情况下应该是有效的 10 OR 10.1 OR 1.23 .. 但这些不应该是有效的 10.123 OR 11.2345 .. 你明白我的意思了吗?
猜你喜欢
  • 2021-10-08
  • 1970-01-01
  • 2013-01-22
  • 2021-01-23
  • 1970-01-01
  • 2016-02-21
  • 2011-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多