0

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
  • Looks like you got two problems. Any reason you’re trying to solve this with regular expressions? Commented Nov 6, 2017 at 4:35
  • If this is a one time validation, not on a live change, then why don't you just write a function to split by - and then by : and do your required validations? You might not RegEx. Commented Nov 6, 2017 at 5:01

2 Answers 2

1

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))

Sign up to request clarification or add additional context in comments.

Comments

0

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$

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.