I have problem with writing regular expression ( php / js ) for Latvian citizen personal codes.
Every citizen in Latvia has own personal code. The format is 11 digits and after the sixth digit there is dash. So first 6 digits are date of birth.
ddmmyy ( at example: 130589 ) then goes dash ( – ) then goes seventh digit, that can be 0, 1 or 2 which means century ( 0 for 19. century and 2 for 21. century ), and then goes digits from 0000 to 9999
Examples:
080189-12602
08 - day
01 - month
89 - year
- - dash
1 - 20th century
2602 - random 4 digit combination.
So question is can someone help me with writing regular expression ?
I recommend that you do not validate the date with the regex. Instead check only for the correct format:
That matches 6 arbitrary digits, a dash, then a digit from
0to2, then 4 more arbitrary digits. Afterwards you will find the date part in capturing group number1, which you can check for a valid date. Alternatively you could extract days, months, years separately:Now the days are in capture
1, the months in capture2, the years in capture3. The validation of the date ranges could in theory be done with the regex but it really not recommended (the regex becomes really bloated and unmaintainable). That is why you should just check that there are digits, and then you can use your programming language (PHP or JS) to check that the given numbers form a valid date.