最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regular Expression: Help Matching a number less than 24 - Stack Overflow

programmeradmin0浏览0评论

so I'm making this regular expression to verify some text boxes on a website that I'm designing for an internship.

The problem is that I'm not so keen on regular expressions, and I'm close to having a working one that matches a number between 0-24 and no more than two decimal places.

This is what I have so far. The pattern is also matching any string; such as, "a" or "az".

var pattern = "^([0-9]{0,2}?.?[0-9]{0,2}|1[0-9].?[0-9]{0,2}|2[0-4].?[0-9]{0,2})$";

so I'm making this regular expression to verify some text boxes on a website that I'm designing for an internship.

The problem is that I'm not so keen on regular expressions, and I'm close to having a working one that matches a number between 0-24 and no more than two decimal places.

This is what I have so far. The pattern is also matching any string; such as, "a" or "az".

var pattern = "^([0-9]{0,2}?.?[0-9]{0,2}|1[0-9].?[0-9]{0,2}|2[0-4].?[0-9]{0,2})$";
Share Improve this question edited Jul 15, 2016 at 14:49 ndnenkov 36.1k9 gold badges79 silver badges107 bronze badges asked Jul 15, 2016 at 14:33 2manyints2manyints 792 silver badges5 bronze badges 11
  • 2 Why would you do this with regex.. You can, of course, if you want to, but this seems easy enough to solve without regex. :-) – Dylan Meeus Commented Jul 15, 2016 at 14:34
  • @DylanMeeus Perhaps OP wants to use the HTML5 input pattern attribute? Apart from that, I would also remend a more readable alternative. – le_m Commented Jul 15, 2016 at 14:35
  • @le_m Yes! that's exactly what I'm using it for in bination with some angular to display warnings to the user when they've input something incorrectly. – 2manyints Commented Jul 15, 2016 at 14:37
  • What is this "a" or "az" that you have mentioned. Please specify what you want the regex to do entirely. – 10100111001 Commented Jul 15, 2016 at 14:39
  • 1 @2manyints How about <input type="number"> with respective boundaries as an alternative? – le_m Commented Jul 15, 2016 at 14:39
 |  Show 6 more ments

5 Answers 5

Reset to default 6

To get a number between 0 and 24 (24 excluded) with optional up to two decimal places:

^(\d|1\d|2[0-3])(\.\d{1,2})?$

The decimal part:

  • \. - match the decimal dot
  • \d{1,2} - one or two digits
  • ()? - makes it optional

The whole part:

  • \d - numbers 0-9
  • 1\d - numbers 10-19
  • 2[0-3] - numbers 20-23
  • (x|y|z) - one of x, y or z

As for the "why is my version matching things like "a" and "az" part" - it's a little plex, but it basically boils down to you using dots (like .?). In regex, a dot means "any one character". To make it match a literal dot, you need to escape it with a slash just like I did.


Minor remark: If you want optional leading zero for single digit numbers, replace 1\d with [01]\d. If you want mandatory leading zero for single digit numbers, replace \d|1\d with [01]\d. If you don't want leading zeroes, leave it as it is.

Assuming you do not want 05 or 5.50

^((?:[0-9]|1[0-9]|2[0-3])(?:\.(?:[1-9]|[0-9][1-9]))?)$

You can try it here

To get a integer number between 1 and 23: ^([1-9]|1[0-9]|2[0-3])$

The following is a quick attempt to match a floating point number from 0 to 24.99 with up to two non-zero digits

^(([0-9])|([01][0-9])|(2[0-4]))(\.[0-9]{1,2})?$ 

I think it might be easier to use math to do this though...

You can see the explanation of the entire regex as well as test it out here. I have also added a few test cases.

^(\d|[01]\d|2[0-3])(\.\d{1,2})?$

Test cases:

Valid:
   22
   1.29
   2.99
   9.99
   13.24
   17.38
   20.01
   02.15
   15.35
   23.56
   1.1

Invalid:
   24.29
   235.215
   21.256
发布评论

评论列表(0)

  1. 暂无评论