douyou3619 2011-10-07 02:08
浏览 35
已采纳

如何使用正斜杠验证DOB字段dd / mm / yyyy [复制]

Possible Duplicate:
How to check if a string is a legal “dd/mm/yyyy” date?

I have a a regular text field that I want to validate that im using for date of birth.

it's like dd/mm/yyyy. Im wondering how i can check that the user entered the date in the correct format with the slashes. I have a php script to calculate the age from the entered data and strips the slashes in order to do so. I figure i'd run with that, but just in case a user enters it wrong, well lol, you know how that is...

Any ideas?

  • 写回答

1条回答 默认 最新

  • dqoag62688 2011-10-07 02:21
    关注
    if (!preg_match('#^(\d{2})/(\d{2})/(\d{4})$#', $date, $matches)) {
        die('Invalid format');
    }
    
    $dob = mktime(0, 0, 0, $matches[2], $matches[1], $matches[3]);
    
    if (date('d/m/Y', $dob) != $date) {
        die('Invalid date');
    }
    
    echo 'User was born on ' . date('l, F jS, Y', $dob);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)