Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 574
  • Last Modified:

Regular expression to validate time

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
0
tia_kamakshi
Asked:
tia_kamakshi
2 Solutions
 
Patrick MatthewsCommented:
^(2[0-3]:[0-5]\d:[0-5]\d|[0-1]\d:[0-5]\d:[0-5]\d$
0
 
Patrick MatthewsCommented:
Sorry, I was missing a ) right before the $
0
 
pauloaguiaCommented:
How about
^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9]):([0-5]?[0-9])$
?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
tia_kamakshiAuthor Commented:
Many Thanks for your quick response
0
 
Hugh FraserConsultantCommented:
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/
0
 
pauloaguiaCommented:
> Don't forget to include the 1:23:45 format (no leading 0).
Yes, that's supported in the expression I posted above ;-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now