Regular Expression to validate Sri Lankan National Identity Card numbers

Published On2019-02-14

I crafted a Regular Expression for Sri Lankan phone numbers before, and here is one to validate Sri Lankan National Identify Card numbers.

Regular Expression

PCRE

Legacy 9-digit numbers

^(?:19|20)?\d{2}(?:[0-35-8]\d\d(?<!(?:000|500|36[7-9]|3[7-9]\d|86[7-9]|8[7-9]\d)))\d{4}(?i:v|x)$

JS flavor

^(?:19|20)?\d{2}(?:[0-35-8]\d\d(?<!(?:000|500|36[7-9]|3[7-9]\d|86[7-9]|8[7-9]\d)))\d{4}(?:[vVxX])$

Explanation

[NIC numbers](https://en.wikipedia.org/wiki/National_identity_card_(Sri_Lanka)

  • 9 digits followed by an English character
  • 11 digits followed by an English character

The linked Wikipedia articles explains the parsing approach.

The above regex:

  1. Validates both new and old NIC number formats
  2. Correctly validates birth year to be in current or the last century
  3. Validates the possible number range for both male and female numbers (do not blame me for binary genders)
  4. Validates the last English letter, where it must be one of v or x that indicates the NIC holder is allowed to vote or not, respectively.