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
2 Answers
Reset to default 9Even 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
版权声明:本文标题:javascript - RegExp for 0:59 to 99:59 or 0 to 99 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745170219a2645928.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论