I am trying to make javascript regex expresion that is taking time input from user. I made expression to take time from 04:00 - 04:59:
/([4]|0[4])[:.,]([0-5][0-9])[.,/;](\d)/
But now when I get to 14:00 - 14:30:
/(14)[:.,]([0-2][1-9]|10|20|30)[.,/;](\d)/
The first regex expression (from 04:00-04:59) is also taking the input from the second one. Is there any chance to separate them, so that there is not problem when user is inputing time?
2 Answers 2
judging by your question, you're not quite ready for regular expressions. also, looking at your question, you're validating time and regex will not make it easy to restrict acceptable values, i.e. 0-24 for hours and 0-59 for minutes
see code snippet for how to do this without regex
consider looking for an existing library that already does it so you don't have to reinvent the wheel.
time = '04:59'
parts = time.split(':')
hours = parseInt(parts[0], 10)
minutes = parseInt(parts[1], 10)
isValid = ((hours >= 0 && hours <=24) && (minutes >= 0 && minutes <= 59))
Comments
You can use regex for validating input from 4:00 to 4:59 is below. Please use this
^(?:0\d[4]|0[4]):[0-5]\d$
-and then by:and do your required validations? You might not RegEx.