【问题标题】:preg_match only nummber, spaces and currency format allowedpreg_match 仅允许数字、空格和货币格式
【发布时间】:2015-08-06 23:05:59
【问题描述】:

在我的 php 中,我使用 preg_match 来验证输入文本。用于货币值的文本框,例如 5000 或 5000.00,或者可以接受空白。

 $pattrn="/^[0-9]{10}$/";

通过使用它只能验证数字。 如何使用 preg_match 过滤这个输入?

【问题讨论】:

  • 试试$pattrn = "/^(?:\d+(?:\.\d+)?)?$/";
  • 嘿,陌生人@AmalMurali ;-)
  • @Amal Murali:空白处呢?
  • 查看编辑后的评论,@machineaddict。

标签: php preg-match


【解决方案1】:

你可以使用:

$pattrn = "/^(?:\d+(?:\.\d+)?)?$/";

解释:

/^             # Assert position at the beginning of the string
(?:            # Begin a non-capturing group
    \d+        #   Match one or more digits from 0-9
    (?:        #   Begin a non-capturing group
        \.\d+  #    Match a dot (.) followed by one more digits from 0-9
    )?         #   Make the capturing group optional
)?             # Make the capturing group optional
$/             # Assert positon at the end of the string

Regex101 Demo

【讨论】:

    【解决方案2】:

    你应该使用 $pattrn="^((?:\d.\d{3}.|\d{1,3}.)?\d{1,3},\d{1,2} )$";

    在此处查看示例preg_match() or similar function for checking currency

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多