0

I want to validate a string which is a duration (time spent) for doing a task:

2h35m
2h 35m

I have a html input and I want to validate it.

I found the solution:

^([0-1]?[0-9]|2[0-3])h([0-5][0-9]m)$

which works for 2h35m

I tried

^([0-1]?[0-9]|2[0-3])[h ]([0-5][0-9]m)$

where the character class [h ] will match the h char or a whitespace, but it does not work ok.

It matches the 2h35m but don't matches 2h 35m.

Any suggestions please? Thanks.


EDIT
a solution for the following formats

2
05
19
23

2h
05h
23h

01h50
1h02m

10h 30m
23h 59m
is here:

^(([0-1]?[0-9]|2[0-3])h?|([0-1]?[0-9]|2[0-3])h ?[0-5][0-9]m?)$

I share a better answer for someone who will need this.

mihai
  • 2,468
  • 3
  • 29
  • 54

0 Answers0