Jobs search - Advertising - Prefessional Book Reviews - Wordpress Themes - WoW Gold

PDA

View Full Version : Time Validator


rafiqasad
Apr 9th 2009, 3:26 am
I need a regular expression that validate time hh:mm:ss. Please guide me

Weirfire
Apr 9th 2009, 4:04 am
Can you share some code so we know what sort of thing will need validating?

SmallPotatoes
Apr 9th 2009, 1:14 pm
You can't easily validate it with a regular expression alone. Maybe it's possible, but not worth the effort I'd say.

Instead, try something like this (untested):
if (preg_match('/^(\d{1,2}):(\d{1,2}):(\d{1,2})$/',
trim($_REQUEST['time']), $matches)
&& $matches[1] < 24
&& $matches[2] < 60
&& $matches[3] < 60)
{
$valid_time = sprintf('%02d:%02d:%02d',
$matches[1], $matches[2], $matches[3]);
echo "<p>Valid time submitted: {$valid_time}</p>";
}
else
echo "<p>Time was invalid.</p>";