最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript: Regular expression for time format HH:MM - Stack Overflow

programmeradmin1浏览0评论

Hi I want to validate a string to check f it has valid time format. HH:MM However,(due to some insane business reasons) I also want to validate 24:00 as correct.

The following works {([0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]} but does not validate 24:00.

Hi I want to validate a string to check f it has valid time format. HH:MM However,(due to some insane business reasons) I also want to validate 24:00 as correct.

The following works {([0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]} but does not validate 24:00.

Share Improve this question edited Feb 3, 2014 at 7:17 dhana 6,5255 gold badges42 silver badges66 bronze badges asked Feb 3, 2014 at 7:14 NEHAVERMA8NEHAVERMA8 2511 gold badge8 silver badges22 bronze badges 3
  • it means that 00:00 would be an invalid time? – zzlalani Commented Feb 3, 2014 at 7:20
  • 1 @zzlalani - Some panies uses a 30-hour system 00:00 to 29:59 :) – Derek 朕會功夫 Commented Feb 3, 2014 at 7:21
  • @Derek朕會功夫.. so they are using 25-hour and 1 second? – zzlalani Commented Feb 3, 2014 at 7:22
Add a ment  | 

4 Answers 4

Reset to default 1

Just change 2[0-3] to 2[0-4]:

^([0[0-9]|1[0-9]|2[0-4]):[0-5][0-9]$

Or if you only want to include 24:00:

^((([0[0-9]|1[0-9]|2[0-3]):[0-5][0-9])|(24:00))$

You can use:

 // Allows times like 24:05:00
  function validateTime(s) {
  var t = s.split(':');

  return /^\d\d:\d\d:\d\d$/.test(s) &&
     t[0] >= 0 && t[0] < 25 &&
     t[1] >= 0 && t[1] < 60 &&
     t[2] >= 0 && t[2] < 60;
 }

Try this,

^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$

Try this Regex:

/^(?:0\d|1[01]):[0-5]\d$/
发布评论

评论列表(0)

  1. 暂无评论