Regular Expression for minimum value 18 and maximum 99 with leading zero. Means my number should be from 18 to 99. With one condition of leading zero i.e 18 == 018, 99 == 099
Asked
Active
Viewed 6,538 times
1
-
2what is your expectation from stackoverflow? – brk May 13 '17 at 06:24
-
1if (v> 17 && v < 100) ? – Mykola Borysyuk May 13 '17 at 06:24
-
I need regular expression for the above question. – Himanshu Shekhar May 13 '17 at 06:26
-
1If your allowable values are 18-99, how does a leading zero fit in to this? – AndrewR May 13 '17 at 06:27
-
@Mykola Borysyuk I want this (v> 17 && v < 100) condition in regex. – Himanshu Shekhar May 13 '17 at 06:28
-
I'd change the value in **column 2 to 8** of your code – Jaromanda X May 13 '17 at 06:29
5 Answers
6
Try this it will help you
Less than 18 and greater than 99 is not allowed
^(1[89]|[2-9][0-9])$

anubhava
- 761,203
- 64
- 569
- 643

Shoaib Quraishi
- 232
- 2
- 17
-
1
-
Am trying to enter value only between 0 to 1000 (including 0 and 1000). but its not working. Am using this regex `/^0?1[01]|0?[0-9][0-9][0-9]$/ ` – Karthikeyan Jul 04 '19 at 18:16
4
^0?1[89]|0?[2-9][0-9]$
- ^ - beginning of string
- 0? - allow beginning 0
- 1[89] - match first character 1 and second 8 or 9
- | - or
- 0? - allow beginning 0
- [2-9] - match first character 2-9
- [0-9] - match second character 0-9
- $ - end of string

AndrewR
- 6,668
- 1
- 24
- 38
1

Gurmanjot Singh
- 10,224
- 2
- 19
- 43
-
well, `0?(1[89]|[2-9]\d)` is just as good :p (you'll also need ^ and $ to truly "validate" – Jaromanda X May 13 '17 at 07:00
-
Sure, but https://regex101.com/r/ht2mil/3 works just as well, with four less characters (i.e. the extra () ()) – Jaromanda X May 13 '17 at 07:17
-
I am glad that worked :) I have recently started learning about regex. – Gurmanjot Singh May 13 '17 at 07:23
0
(01[89])|(0[2-9]\d)
(01[89])
matches 018 to 019(0[2-9]\d)
matches 020 to 099

voskhod
- 194
- 12
-
no, that requires a leading 0, not optional as stated in the request for code - I mean, question – Jaromanda X May 13 '17 at 06:57
-
-
I took `With one condition of leading zero` to mean, in English `with one optional leading zero` - but you're probably right :p – Jaromanda X May 13 '17 at 07:05
0

Abhishek Gurjar
- 7,426
- 10
- 37
- 45
-
`[0-9]+` a) what's wrong with \d - and b) that would allow, say 029631027910246741584 – Jaromanda X May 13 '17 at 06:58
-
@JaromandaX You are right on b) part I am working on it for \d see here http://stackoverflow.com/questions/6479423/does-d-in-regex-mean-a-digit – Abhishek Gurjar May 13 '17 at 06:59
-
ahh, I see the issue with \d (though, not sure how JS handles \d but you have an excellent point about it) – Jaromanda X May 13 '17 at 07:02