6

I'm still a beginner at regex so this is a little above me currently.

I need to validate a number input to two characters and it can't be more than the value 12.

The two numbers easy:

/^\d{1,2}$/

Then check that if there are two numbers then the first must be either 0 or 1, and the second must be either 0, 1, or 2. That part I don't know how to express, regularly...

And also if possible I would like to have it in single regex statement.

Any help appreciated.

Martej
  • 55
  • 1
  • 6
Forteasics
  • 121
  • 1
  • 2
  • 10
  • 1
    I doubt regex is the best way to do this. What language are you using? – lonesomeday Apr 13 '11 at 10:41
  • So, e.g. `09` is not allowed? It has two characters and the second one is not in `[012]`. – Piskvor left the building Apr 13 '11 at 10:43
  • It's just a simple month validation in numerical input, validated by an already extensive js validation engine, reason for regex is that i need some practise with regex and if not now then when, and now im simply adding to the already existing rules. Im not using the values for calculations, only date input so character validations seems sufficient. Thanks all for the responses. – Forteasics Apr 13 '11 at 11:10
  • in case you're looking to check a number below a length: http://stackoverflow.com/questions/42328875/javascript-regex-to-remove-all-numbers-with-specific-lenght-or-do-a-persistent – cregox Feb 19 '17 at 15:13

6 Answers6

15

Regex is not suited to dealing with ranges like this, but anyway this should work:

/^(0?[1-9]|1[012])$/

Description:

^       match start of the string
(0?     optional zero
[1-9]       any number between 1 and 9
|1[012])    or match 1 followed by either a 0, 1, 2 i.e. 10, 11, 12.
$           match end of string
Gary Green
  • 22,045
  • 6
  • 49
  • 75
  • But add an optional 0? in front of the first part just in case – frisco Apr 13 '11 at 10:44
  • There, I've updated the regex. Should allow for 01, 02, 03, etc. I assume this is to validate a month date? ;-) If you don't want to allow for 012,013 etc then just move the 0? to inside the first parentheses: /^(0?[1-9]|1[012])$/ – Gary Green Apr 13 '11 at 10:48
  • I know that is a little too much for regex in a validation. but works really good. I extended to validate a full date, and it works great for a raw verification. I did not make a conditional verification for months and days, but here it comes, hope it helps someone: `@"^[0-9]{4}-(0?[1-9]|1[012])-([0-2]?[1-9]|3[01])\s([0-1]?[1-9]|2[0-3]):([0-6]?[1-9]):([0-6]?[1-9])$"` – marcelo-ferraz Oct 08 '12 at 19:28
1

For something like this (testing if an integer is less than a given value), you should really not use a regex.

Instead, just use a simple condition like this one (the syntax will depend on the language you're working with) :

if ($variable <= 12) {
  // OK
}
else {
  // Not OK
}
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • I'm using a js validation engine (Form Validation Engine 2.0) so to write another check or modify the regex is simple enough, but i'd rather expand on regex since im weak at it. – Forteasics Apr 13 '11 at 10:47
0

0[0-9]|1[0-2]

Description:

 [0-9] : number between 00 and 09
     | : or
1[0-2] : and if starting with 1, next number should be between 0 -1 (testing 10-12)
falopsy
  • 636
  • 7
  • 16
0

For the "character either x, or y, or z, or ...", there's the character class, expressed in square brackets:

[012]

means "any character of 0,1, or 2".

Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
0
(0[0-9])|(1[0-2])

this matches a zero, followed by one arbitrary digit, or a one, followed by 0, 1 or 2

Frank Schmitt
  • 30,195
  • 12
  • 73
  • 107
0

The following code (Groovy) creates a regex similar to the answer from @Gery Green but works for any given number:

String getSmallerNumberRegEx(String number) {
  int length = number.length()
  List<String> patterns = [];
  if (length >= 2) {
    patterns.add("\\d{1,${length - 1}}") // any number with less digits
  }
  for (int i = 0; i < number.length(); i++) {
    def digit = number[i].toInteger()
    if (digit > 0) {
      // same prefix + smaller number + any postfix:
      patterns.add("${number.substring(0, i)}[0-${digit - 1}]\\d{${length - i - 1}}")
    }
  }
  return patterns.join('|')
}
daniel-sc
  • 1,139
  • 11
  • 23