验证输入是否采用此时间格式-"HH:MM" [英] Validate that input is in this time format - "HH:MM"

查看:63
本文介绍了验证输入是否采用此时间格式-"HH:MM"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用preg_match验证时间输入是否采用以下格式-"HH:MM"

I'm trying to use preg_match to validate that a time input is in this format - "HH:MM"

推荐答案

您可以使用正则表达式进行检查.

You can use regular expressions to check that.

12小时:

preg_match("/^(?:1[012]|0[0-9]):[0-5][0-9]$/", $foo)

24小时:

preg_match("/^(?:2[0-3]|[01][0-9]):[0-5][0-9]$/", $foo)

如果您使用从 01:00 24:59 的24小时制,请使用

If you use a 24-hour clock going from 01:00 to 24:59, use

preg_match("/^(?:2[0-4]|[01][1-9]|10):([0-5][0-9])$/", $foo)

这篇关于验证输入是否采用此时间格式-"HH:MM"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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