Jump to content

Help with regex


robsim

Recommended Posts

I took what Ken2k7 posted and fixed it. This is tested:

 

function validTime($time)
{
    return preg_match("/^((0?[0-9])|(1[0-9])|(2[0-3]))(:[0-5][0-9]){2}$/", $time);
}

 

To be valid the time must follow the following parameters:

 

The beginning must be one of the following

(0-9) (any single digit number)

0(0-9) (0 followed by any single digit number)

1(0-9) (1 followed by any single digit number)

2(0-3) (2 followed by 0, 1, 2 or 3)

 

There must then be a colon followed by

(0-5)(0-9) (first digit a 0, 1, 2, 3, 4, or 5, second digit any number)

 

Then another colon followed by

(0-5)(0-9) (first digit a 0, 1, 2, 3, 4, or 5, second digit any number)

 

One problem with the above is that it accepts 00:00:00 but not 24:00:00. Some people prefer one over the other. If you need to support 24:00:00, then change the regex to this:

 

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

Link to comment
https://forums.phpfreaks.com/topic/198088-help-with-regex/#findComment-1039571
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.