1

I am new to Regex and am trying to create a basic date pattern for a text input field in HTML.

For now I only want to consider the following rules:

  • first number group with 2 digits from 01 to 31
  • second number group with 2 digits from 01 to 12
  • third number group with 4 digits from 1961 to 2100
  • separator after first and second number group must be a dot / period

Allowed examples:

  • 01.12.1990
  • 04.05.2000
  • 31.12.2010

I tried the following but this does not work for me.
Can someone tell me what I am missing here ?

My Regex pattern:

pattern="[01-31].[01-12].[1961-2100]"

Update:

pattern in my case is the HTML pattern attribute not a variable in JS.

Many thanks in advance,
Tom

keewee279
  • 1,656
  • 5
  • 28
  • 60

3 Answers3

1

try this :

pattern = "(0[1-9]|1[0-9]|2[0-9]|3[0-1])\.(0[1-9]|1[0-2])\.(\d){4}";
Escanor
  • 95
  • 3
  • 9
  • Thanks a lot. This helps but it's missing the range for the last number group and it's not working for me. Note: pattern in my case is the HTML pattern attribute not a variable in JS. – keewee279 May 17 '20 at 06:03
  • 1
    im sorry! so try this instead of (\d) : (19[6-9][0-9]|20[0-9]{2}|2100) – Escanor May 17 '20 at 06:21
1

Try with this regex: (0[1-9]|[12]\d|3[0-1])[\./]([0[1-9]|1[0-2])[\./](196[1-9]|19[7-9]\d|20\d\d|2100)

Demo here

Community
  • 1
  • 1
GolamMazid Sajib
  • 8,698
  • 6
  • 21
  • 39
0

I found a solution that works for me:

pattern="(0[1-9]|[12][0-9]|[3][0-1])[\.]0*([1-9]|1[0-2])[\.](196[1-9]|19[7-9][0-9]|20[0-8][0-9]|209[0-9])"

Thanks,
Tom

keewee279
  • 1,656
  • 5
  • 28
  • 60