PHP正则表达式日期验证 [英] Php regex date validation

查看:55
本文介绍了PHP正则表达式日期验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用正则表达式来验证日期输入.

I'm trying to validate a date input by the use of regex.

if(!preg_match("/^[0-9]{4}\/[0-9]{2}\/[0-9]{2}$/", $_POST['variant']['sales_start'])) { 
  echo "invalid";
}

我尝试输入的字符串是 2011-02-03,但是它失败了,我似乎无法弄清楚原因.

The string I'm trying to input is 2011-02-03, however it's failing, and I can't seem to figure out why.

谁能告诉我我做错了什么?

Can someone please tell me what I'm doing wrong?

提前致谢

推荐答案

你用破折号分隔日期而正则表达式寻找斜线?

You're separating the date with dashes and the regex is looking for slashes?

试试

if ( !preg_match( "/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/", $_POST['variant']['sales_start'] ) )
{ 
    echo "invalid";
}

这篇关于PHP正则表达式日期验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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