【问题标题】:Need help for a regular expressions with PHP Preg_match to validates a number field on our form需要帮助使用 PHP Preg_match 的正则表达式来验证表单上的数字字段
【发布时间】:2019-05-25 06:18:29
【问题描述】:

:) 我们想设置一个特殊条件(基于 PHP Preg_match 正则表达式)来验证表单上的数字。

  • “数字字段”最初最多只能包含 13 个数字(并且只有数字。没有字母或其他任何内容)。

  • 第一个数字必须是(仅)“1”或“2”(不是其他)

  • 第4个和第5个数字代表(2个数字组合)某人的“出生月份”,所以第4个数字需要是“0”或“1”,而第5个需要在“ 1”和“9”。

非常感谢您能帮助我们,为 PHP Preg_match 中的正则表达式提供良好的“语法”,以验证表单上的该字段! :)

感谢社区的支持和帮助!

问候

【问题讨论】:

    标签: php preg-match


    【解决方案1】:

    这是您向我们描述的文字正则表达式模式:

    ^[12]\d{2}(?:0[1-9]|1[0-2])\d{8}$
    

    示例脚本:

    $input = "1231212345678";
    if (preg_match("/^[12]\d{2}(?:0[1-9]|1[0-2])\d{8}$/", $input)) {
        echo "MATCH";
    }
    

    这个正则表达式模式说:

    ^                  from the start of the string
    [12]               match 1 or 2 as the first digit
    \d{2}              then match any digits in the 2nd and 3rd position
    (?:0[1-9]|1[0-2])  match 01, 02, ..., 12 as the two digit month
    \d{8}              then match any other 8 digits
    $                  end of string
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      相关资源
      最近更新 更多