Part of a recent project required capturing user details for HMRC.

Included in these details are, of course, the user’s National Insurance (NI) number.

We needed to validate the form and ensure that the submitted NI number was valid. However, many sources for NI number formats that we discovered online had failed to consider an important fact; temporary NI numbers exist.

A temporary NI number is used before a candidate has been issued their permanent NI number, and follows the following format, where DD MM YY is your birth date and M/F your gender;


Therefore, a complete regular expression for national insurance numbers that accommodates the format of temporary numbers looks like the following:


If you’re interested, the regular expression breaks down like so:

This matches against “normal” NI numbers, according to the following government source:

This part matches against temporary numbers.

The pipe between the two, of course, means “OR”. So, we’re essentially saying “Match a real NI number OR match a temporary one”.

The above character sequence means spaces are optional, so a NI number can either be input as “XX 00 00 00 X” or “XX000000X” without issue.

This should be fairly straight forward, it means match two characters in sequence, each between 0 and 9.

And the rest of the matching sets should also be self-explanatory, where we are defining the character sets that are valid in an NI number.