preg_match:检查生日格式(dd/mm/yyyy) [英] preg_match: check birthday format (dd/mm/yyyy)

查看:107
本文介绍了preg_match:检查生日格式(dd/mm/yyyy)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何使表达式检查生日输入以匹配类似dd/mm/yyyy的格式?下面是到目前为止我得出的结果,但是如果我输入99/99/9999,也需要这样做!

How do I make the expression which checks the birthday input to match a format like this dd/mm/yyyy? Below is what I came out so far, but it takes this too if I put 99/99/9999!

if (!preg_match("/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/", $cnt_birthday))
  {
   $error = true;
   echo '<error elementid="cnt_birthday" message="BIRTHDAY - Only this birthday format - dd/mm/yyyy - is accepted."/>';
  }

如何确定dd的值只有01到31,mm的值只有01到12?但是我确定如何限制yyyy ...我认为理论9999应该可以接受...如果您有更好的主意,请告诉我!

How can I make sure that its only 01 to 31 for dd and 01 to 12 for mm? but I am sure how to restrict yyyy... I think theoritical 9999 should be acceptable... let me know if you have a better idea!

谢谢, 刘

推荐答案

基于 checkdate 的解决方案:

Based on Tim's checkdate based solution:

使用explode可以轻松地提取日,月和年:

The extraction of day, month and year can easily be done using explode as:

list($dd,$mm,$yyyy) = explode('/',$cnt_birthday);
if (!checkdate($mm,$dd,$yyyy)) {
        $error = true;
}

这篇关于preg_match:检查生日格式(dd/mm/yyyy)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆