0
^(19|20)?[0-9]{6}[- ]?[0-9]{4}$

Currently I use the following pattern for a number pattern.

The pattern works for numbers like this..

20010205-1234 or 010205-1234. The first 4 or 2 digits indicate the year a person is born. And now I need to change this so that one can only enter year between 1900 - 2006. If you for example enter 20070305-1234 it should not be allowed. But 20060305-1234 should be allowed

MTplus
  • 2,077
  • 4
  • 34
  • 51
  • Can you use separate components for the date parts (month, year, day) and only allow full years to be typed? Because in that case you could use `min="2005"`. Otherwise, you have to do this with javascript. – Federico klez Culloca Oct 22 '19 at 09:23
  • `pattern="(?:200[6-9]|20[1-9]\d|2[1-9]\d{2})\d{4}[- ]?\d{4}"`. It is not quite clear with years without the first two digits though. – Wiktor Stribiżew Oct 22 '19 at 09:27
  • Maybe try `pattern="(?:(?:20)?(?:0[6-9]|[1-9]\d)|(?:2[1-9])?\d{2})\d{4}[- ]?\d{4}"` – Wiktor Stribiżew Oct 22 '19 at 09:30
  • Sorry I see that I described it a bit wrong in the original description but I have corrected that now. – MTplus Oct 22 '19 at 09:41
  • Should the pattern also validate the dates earlier than this limit, e.g. should 20040230-2561 be considered valid or not? – Ilya Streltsyn Oct 22 '19 at 10:47
  • Only years between 1900 - 2006 should be allowed, if birth year is 2007 or higher its not valid. So 20040230-2561 is valid – MTplus Oct 22 '19 at 11:17
  • Looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/a/2759417/3832970) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Oct 22 '19 at 11:24
  • This seem to work ^(19[5-8][0-9]|199[0-9]|200[0-6])\d{4}[- ]?[0-9]{4}$ this allows years between 1950-2005 – MTplus Oct 22 '19 at 11:57

0 Answers0