admin管理员组

文章数量:1414613

I want the strict regular expression for 0:59 to 99:59 Hours. It should allow 0 to 99 Hours. How could it be done.


/^([0-9]{1,2}(\:[0-5][0-9])?)$/

is working properly

I want the strict regular expression for 0:59 to 99:59 Hours. It should allow 0 to 99 Hours. How could it be done.


/^([0-9]{1,2}(\:[0-5][0-9])?)$/

is working properly

Share Improve this question edited Jun 29, 2011 at 11:42 Dori 9251 gold badge12 silver badges20 bronze badges asked Jun 15, 2011 at 8:38 MayureshPMayureshP 2,6347 gold badges33 silver badges41 bronze badges 5
  • 0:59, 1:00, 1:01, ... 99:00, ..., 99:59? Is 0:1 allowed? – Lekensteyn Commented Jun 15, 2011 at 8:41
  • i tried /^\d{1,2}(\:([0-5][0-9]))?$/ – MayureshP Commented Jun 15, 2011 at 9:08
  • 1 @MayP: Try it this way /\d{1,2}(:[0-5]\d)?/ – niksvp Commented Jun 15, 2011 at 9:16
  • @niksvp: please post ur answer,this will be accepted answer – MayureshP Commented Jun 15, 2011 at 10:28
  • @MayP - Thanks, I posted it.. ;) – niksvp Commented Jun 15, 2011 at 12:12
Add a ment  | 

2 Answers 2

Reset to default 9

Even something as simple as \d{1,2}(:[0-5]\d)? should suffice.

\d                 A digit
\d{1,2}            One or two digits
\d{1,2}:           One or two digits followed by :
\d{1,2}:[0-5]      One or two digits followed by : followed by a digit 0 to 5
\d{1,2}:[0-5]\d    ...followed by a digit (0 to 5) and another digit
\d{1,2}(:[0-5]\d)? ...making the :XX part optional due to the ?

Second update: Fixed to account for the optional :XX part.

Try it this way /\d{1,2}(:[0-5]\d)?/

This regex will also validate numbers from 0 to 99 with or without : and post data. :)

UPDATE

javascript code for the same would be like

var field1 = "0:00"
var regTime = /\d{1,2}(:[0-5]\d)?/ ;
if(field1 == field1.match(regTime)[0]){ alert('matches') }

本文标签: javascriptRegExp for 059 to 9959 or 0 to 99Stack Overflow