10

I'm in need of a regex, which takes a minimum and a maximum number to determine valid input, And I want the maximum and minimum to be dynamic.

I have been trying to get this done using this link https://stackoverflow.com/a/13473595/1866676 But couldn't get it to work. Can someone please let me know how to do this.


Let's say I want to make a html5 input box, and I Want it to only receive numbers from 100 to 1999

What would a regex for this like this look like?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
DaCh
  • 921
  • 1
  • 14
  • 48
  • 2
    Could you please update your question with 1) your sample input with expected behavior 2) what you exactly tried and how 3) and of course your programming language. – Wiktor Stribiżew Jun 08 '16 at 10:50
  • I suggest you have a look at [How do I write a regular expression that matches an IPv4](https://blogs.msdn.microsoft.com/oldnewthing/20060522-08/?p=31113/). It isn't an answer (or even a forum) but it's fun to read and highlights why you can do many things with regular expressions, but you shouldn't always do them. As about your question, if numbers are not fixed you clearly cannot write a static regular expression and if you can use a programming language to build the expression you can probably use that language to just compare numbers in a simpler way. – Álvaro González Jun 08 '16 at 10:59
  • @WiktorStribiżew tried updated it as you requested – DaCh Jun 08 '16 at 11:00
  • See http://www.regular-expressions.info/numericranges.html for example. It can be done, but it's probably not something you want to do. It would be better to extract the number from whatever string you are parsing and then check if it's within the specified range after. – chocochaos Jun 08 '16 at 11:01
  • @DaCh You wrote his name in your question, but didn't really supply any of the three points ;-) – Álvaro González Jun 08 '16 at 11:01
  • @ÁlvaroGonzález it's true that some "native" comparing of number would be easy, but then i would need to the code for all my apps, where instead i could send the regex within the requested to my server, – DaCh Jun 08 '16 at 11:03
  • @ÁlvaroGonzález I think i did, 1. your sample input with expected behavior = And i Want it to only receive numbers from 100 to 1999. 2.I have been trying to get this done using this link http://stackoverflow.com/a/13473595/1866676 and 3. Let say i want to make a html5 input box. but if i misunderstod something, i will of curse try to change it. – DaCh Jun 08 '16 at 11:04
  • @ÁlvaroGonzález Check my answer, it explains how to do it. It's not too difficult or impossible – yosefrow Jun 08 '16 at 11:27
  • @yosefh Check my link, it explains why not to do it (your answer assumes a fixed range anyway so my concerns remain the same). – Álvaro González Jun 08 '16 at 11:34

3 Answers3

20

First off, while it is possible to do this, I think if there is a simpler way to choose a number range such as <input type="number" min="1" max="100">, that way would be preferred.

Having said that, here's how the kind of regex you requested works:

ones: ^[0-9]$  // just set the numbers -- matches 0 to 9
tens: ^[1-3]?[0-9]$ //set max tens and max ones -- matches 0 to 39
tens where max does not end in 9 ^[1-2]?[0-9]$|^[3][0-4]$ // 0 to 34
only tens: ^[1][5-9]$|^[2-3][0-9]$|^[4][0-5]$ // 15 to 45

Here, lets pick an arbitrary number 1234 to 2345

^[1][2][3][4-9]$|
^[1][2][4-9][0-9]$|
^[1][3-9][0-9][0-9]$|
^[2][0-2][0-9][0-9]$|
^[2][3][0-3][0-9]$|
^[2][3][4][0-5]$

https://regex101.com/r/pP8rQ7/4

Basically the ending of the middle series always needs to be a straight range that can reach 9 unless we are dealing with the ones place, and if it cant, you have to build it upwards toward the middle each time we have a value that can't start in 0 and then once we reach a value that cant end in 9 break early and set it in the next condition.

Notice the pattern, as each place solidifies. Also keep in mind that when dealing with going from lower to higher places, optional operators ? should be used.

Its a bit complex, but its nowhere near impossible to design a custom range with a bit of thought.

If you are more specific, we can craft an exact example, but this is generally how it is done:beginning-range|middle-range|end-range You should only need beginning or end-ranges in certain cases like if the min or max does not end in 9. the ? means that the range that comes after it is optional. (so for example in the first case it lets us have both single and double numbers.

so for 100 - 1999 it's quite simple actually because you have lots of 9's and 0's

/^[1-9][0-9][0-9]$|^[1][0-9][0-9][0-9]$/

https://regex101.com/r/pP8rQ7/1

Note: Single values don't need ranges [n] I just added them for readability.

Edit: There is a new regex range generator at: https://3widgets.com/

yosefrow
  • 2,128
  • 20
  • 29
  • 1
    `Here, lets pick an arbitrary number 1234 to 2345` if in understand it. if i use that regex, i would be able to enter 2344? am i right? – DaCh Jun 08 '16 at 11:30
  • 1
    ugh wait i made a mistake - fixing – yosefrow Jun 08 '16 at 11:33
  • 1
    You have my +1 for the information but this expression needs to be placed somewhere and it's in the larger picture where the whole approach starts flickering. If you put it in the `` attribute you've written a convoluted replacement for `max/min`. If you validate with JavaScript you've written a convoluted replacement for `<` and `>`. And so on. – Álvaro González Jun 08 '16 at 11:42
  • 1
    @ÁlvaroGonzález I completely agree. This is more trivia and invention than anything else. I had fun coming with it :D – yosefrow Jun 08 '16 at 11:44
  • @ÁlvaroGonzález sry, but ain't you saying it may not be 2340 ? – DaCh Jun 08 '16 at 11:51
  • @DaCh thanks for catching that - I updated the post - So yeah this is definitely more difficult than it needs to be :P – yosefrow Jun 08 '16 at 11:54
  • @ÁlvaroGonzález I'm sry, for a couple of seconds i mistake you guys from each other. – DaCh Jun 08 '16 at 12:01
  • @yosefh Thanks a bunch, this was what i was looking for. thanks for this nice explained answer! – DaCh Jun 08 '16 at 12:02
  • @DaCh You're very welcome, but just remember that is preferable to use simple > and < operators along with min and max numbers. such as if (x >= min and x <= max) – yosefrow Jun 08 '16 at 12:09
3

Essentially, you can't.

For every numeric range, there exists a regex that will match numbers in that range, therefore it is possible to write code that can generate a such regex. But such a regex is not a simple reformatting of the range ends.

However, such code would require colossal effort and complexity to write compared to code that simply checked the number using numeric methods.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 1
    Not an expert on this subject, but since it is html5 he needs the regex to filter it. If not he would have to pass the values to another page to validate them? `^[1-9][0-9][0-9]$|^1[0-9][0-9][0-9]$` would catch it all? – Andreas Jun 08 '16 at 11:15
  • @Andreas you are correct. I've developed a system for this. it's easier than one might think. Check out the answer below Although it definitely might be overkill in some situations as Bohemian has suggested – yosefrow Jun 08 '16 at 11:30
  • Thanks for suggesting this. I've decided to change my approach rather than coming up with a regex. – pbreach Jul 02 '18 at 20:06
2

With HTML 5 simply put a range input...

<form>
  Quantity (between 100 and 1999):
  <input type="number" name="quantity" min="100" max="1999">
</form>

with regex:

^([12345679])(\d)(\d)|^(1)(\d)(\d)(\d)

So if you need to create the regex dinamically it's possible but a bit tricky and complex

MrPk
  • 2,862
  • 2
  • 20
  • 26
  • Out of curiosity... Can plain HTML actually enforce the restriction, or will merely style the element accordingly? – Álvaro González Jun 08 '16 at 11:38
  • The value will be constrained but Note: The max and min attributes of the input tag is not supported in Internet Explorer 9 and earlier versions.-- Note: The max and min attributes will not work for dates and time in Internet Explorer 10 (and newer), or Firefox, since IE 10+ and Firefox does not support these input types.--Note: Anyone can fake client-side validation, so its always important to validate on the server also http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_max_min – yosefrow Jun 08 '16 at 11:50
  • of course any input can be fake client-side, so you need a validation server side (javascript can be skipped with more difficoulty but skipped as normal input). – MrPk Jun 08 '16 at 12:03