-4

I have a RegEx Expression:

^((0[1-9]|1[012])[- /.]?(0[1-9]|[12][0-9]|3[01])[- /.]?(19|20)\d\d)|((((0?[13578])|(1[02]))[- /.]?((0?[1-9])|([12][0-9])|(3[01]))|((0?[469])|(11))[- /.]?((0?[1-9])|([12][0-9])|(30))|(0?[2])[- /.]?((0?[1-9])|([1][0-9])|([2][0-8])))[- /.]?(19\d{2}|20\d{2}))|(((0?[2]))[- /.]?((0?[1-9])|([12][0-9]))[- /.]?((19|20)(04|08|[2468][048]|[13579][26])|2000))$

I want to make sure that it's at least 8 characters long. I have tried this: {8,}, .{8,} and variations of parens around the RegEx. I'm probably missing something.

Some of the places I've looked for answers: Here And Here

Those options still allow 6 characters when I need 8 or more Here is how I've been testing it

Community
  • 1
  • 1
Kevin Fischer
  • 371
  • 7
  • 16

1 Answers1

2

There is a couple issues to fix:

  • alternations (|) have priority over anchors (^$). To use it well, you must surround it with parenthesis: ^(a|b)$
  • To check the length in a regex, you can use a lookahead ((?=)): (?=.{8,}). It will ensure that the string is a least 8 char longs.

Combining that you get this working example.

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142