正则表达式NIC [关闭]

Can you please help me in validation a mauritian NIC.

[1 capital letter][dateofBirth][6 numbers][1 capital letter]

Note: date of birth format: ddmmyy

I tried this one:

/^[A-Z]{1}[0-9]{12}[A-Z]{1}$/

But how do i validate the date of birth in the NIC? Thank you

If you want to validate the date, you'll have a hard time doing so just with regex. But, fortunately, PHP has a checkdate function. You just have to extract the day, month and year, which should be pretty easy but will slightly change your regex. Don't forget to put parenthesis around each element (day, month, year), to be able to retrieve them.

If I'm not mistaken, your regex should be something like that :

/^[A-Z]([0-9]{2})([0-9]{2})([0-9]{4})[0-9]{6}[A-Z]$/

Then check the date, so:

preg_match('/^[A-Z]([0-9]{2})([0-9]{2})([0-9]{2})[0-9]{6}[A-Z]$/', 'D160289784495A', $match);

var_dump(checkdate($match[2], $match[1], $match[3]));

Not for range of dates, years and months.

You cannot simply check ranges of month, days and years with regex. For that you will have use some library function. Following regex will do a preliminary job of validating the format of NIC. After that proceed to checking the validity of dates.

This regex should check the format ^[A-Z]\d{12}[A-Z]$

Regex101 Demo

Note that in your format

[1 capital letter][dateofBirth][6 numbers][1 capital letter]

Since format of date of birth is ddmmyy it's counted only as 6 numbers so your format is actually like this

[1 capital letter][12 numbers][1 capital letter]