Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript regexp for numbers between 00-59 (seconds)

I want to check if a field is a valid time value (just seconds). So I want to accept the numbers from 0 to 59. I came out with this:

[0-5][0-9]?

which almost does the job. But excludes the digits 7-8-9... It works if the user digit 07, but I don't want to force the user to digit the first 0. So I tried something like this:

([0-5][0-9]? | [0-9]) 

but this does not works and produces an error of too many recursive calls.

Any idea?

like image 499
Segolas Avatar asked Jan 14 '23 06:01

Segolas


2 Answers

In your 2nd regex, you need to remove that ? from the first part, and make it [1-5] instead of [0-5]:

[0-9]|[1-5][0-9]

And if you want to be flexible enough to allow both 7 and 07, then use [0-5]:

[0-9]|[0-5][0-9]  

And then, simplifying the above regex, you can use:

[0-5]?[0-9]   // ? makes [0-5] part optional
like image 103
Rohit Jain Avatar answered Jan 16 '23 21:01

Rohit Jain


This should be sufficient: [0-5]?\d

However if you want to enforce two digits (ie. 01, 02...) you should just use [0-5]\d

like image 41
Niet the Dark Absol Avatar answered Jan 16 '23 20:01

Niet the Dark Absol