Regular expression to validate time

tia_kamakshi
tia_kamakshi used Ask the Experts™
on
Hi,

I wanted to validate time by regular expression in hh:mm:ss format

for example 16:12:18 is valid time

The below reqular expression is not working good

^([0]\d|[1][0-2])\/([0-2]\d|[3][0-1])\/([2][01]|[1][6-9])\d{2}(\s([0]\d|[1][0-2])(\:[0-5]\d){1,2})*\s*([aApP][mM]{0,2})?$

Can you please help me in fixing the regular expression

User has to enter time in hh:mm:ss format only

Many Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010
Commented:
^(2[0-3]:[0-5]\d:[0-5]\d|[0-1]\d:[0-5]\d:[0-5]\d$
Top Expert 2010

Commented:
Sorry, I was missing a ) right before the $
How about
^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9]):([0-5]?[0-9])$
?
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Author

Commented:
Many Thanks for your quick response
Hugh FraserConsultant

Commented:
Don't forget to include the 1:23:45 format (no leading 0).

/2[0-3]:[0-5]\d:[0-5]\d|[0-1]\d:[0-5]\d:[0-5]\d|\s\d:[0-5]\d:[0-5]\d/
> Don't forget to include the 1:23:45 format (no leading 0).
Yes, that's supported in the expression I posted above ;-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial