【问题标题】:Regular Expression and Logic for Date Validation [closed]日期验证的正则表达式和逻辑
【发布时间】:2014-08-24 02:04:36
【问题描述】:

我有这个日期验证代码:

if(!preg_match('/^[0-3]?[0-9]\/[0-1]?[0-9]\/[0-9]{4}$/',$Date))
{
     echo "<link rel=stylesheet type=text/css href=style.css /><b><font color=red size=2>Please Check Preferred Date </font><a href=index.php> Click here to Go Back .</a> "; 
     exit();
}

但是当我传递格式为dd/mm/yyyy 的日期时,它会显示错误消息。正则表达式有问题吗?

【问题讨论】:

  • 你在哪里找到这个代码?

标签: php validation date


【解决方案1】:

试试这个。 (dd/mm/YYYY) 例如:06/12/2014

   if (preg_match('\d{1,2}/\d{1,2}/\d{4}', $input))
   {
      return true; // it matched, return true
   }
   else
   {
      return false;
   }

【讨论】:

  • 实际上它是如何工作的 ...if(!preg_match('/^[0-3]?[0-9]\/[0-1]?[0-9]\/[ 0-9]{4}$/',$Date))
  • 你到底需要什么...解释清楚...
  • 函数是如何工作的(preg_match)
  • 那么99/99/20140/0/2015 是有效日期吗?
【解决方案2】:

有效。

$Date = '25/12/2014';
if(!preg_match('/^[0-3]?[0-9]\/[0-1]?[0-9]\/[0-9]{4}$/',$Date))
{
    echo "<link rel=stylesheet type=text/css href=style.css /><b><font color=red size=2>Please Check Prefered Date 124</font><a href=index.php> Click here to Go Back .</a> "; 
    exit();
}

你可以在这里测试:http://writecodeonline.com/php/

【讨论】:

  • 实际上它是如何工作的... if(!preg_match('/^[0-3]?[0-9]\/[0-1]?[0-9]\/[ 0-9]{4}$/',$Date))
猜你喜欢
  • 1970-01-01
  • 2013-04-22
  • 2011-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多