NI Number Validation

From TekiWiki
Revision as of 08:51, 25 March 2016 by WikiSysop (Talk | contribs) (1 revision imported)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Validating National Insurance Numbers with Regular Expressions

Regular Expressions make a compact way to verify the format of the UK National Insurance Number.

The format of UK National Insurance Numbers are described on the HMRC (Her Majesty's Revenue and Customs) web site under the label NIM39110. The format has changed from time to time, and there are some specialist formats, so depending on your purpose you may need to alter the expressions given here.

The basic format is AA999999A, and a check for this format would be:

[A-Z]{2}[0-9]{6}[A-Z]

If you are looking for National Insurance Numbers within some text and want to replace them with dummy text, this simple expression will probably meet your needs and catch some of the more exotic NI format numbers that might also need to be replaced.

However, if we want to validate NI numbers say for payroll purposes, we can be more specific than this basic format, there are a few further rules:

The suffix letter for a personal NI number is restricted to A, B, C or D. Historically, temporary numbers allowed M or F to indicate gender, but these are no longer accepted by HMRC.

The pensions only prefixes can have other suffixes - NC used to accept suffixes M or F, and the current PP prefix can have a suffix of P. These are not allowed in the HMRC quality standard and so are not allowed in the expressions here.

The first letter cannot be D, F, I, Q, U or V. The second letter cannot be D, F, I, O, Q, U and V.

[ABCEGHJ-PR-TW-Z][ABCEGHJ-NPR-TW-Z][0-9]{6}[A-D]

Additionally, there are certain combinations of prefix letters that have been used for administrative purposes and so are not assignable to individuals on a payroll:

NI Prefix Usage Effect on Expression
OO Used for Tax Credits where no NINO is held at the start of the tax credit claim. None. "O" is not permitted as the first letter of the prefix.
CR Used within HMRC for Account Investigations Section in NIC&EO. Exception - (C[ABCEGHJ-NPSTW-Z])
FY Was used for Attendance Allowance claims None. "F" is not permitted as the first letter of the prefix.
MW Was used January 1980 and January 1987 for Migrant Workers Exception - (M[ABCEGHJ-NPRSTX-Z])
NC Was used by Stakeholder Pensions administrator to allocate a dummy identifier in the format:

NCddmmyyF

ddmmyy is the birth date and F (or M) the gender of the individual. No longer accepted.

Exception - (N[ABEGHJ-NPRSTW-Z])
PP Used as a pension temporary NI number - still accepted in quality standard, but is not acceptable for payroll purposes. Exception - (P[ABCEGHJ-NRSTWX]) - this exception covers PY and PZ as well.
PY, PZ Was used for tax only cases from 1970's until August 2002. Numbers no longer accepted. See above
TN Temporary Numbers used by employers and DWP when an individual's NINO was unknown.

The format was: TNddmmyyF

ddmmyy is the birth date and F (or M) the gender of the individual. This format is no longer accepted.

Exception - (T[ABCEGHJ-MPRSTW-Z])

We can exclude these administrative prefixes by adding a specific expression for each prefix, e.g. allowing CA, but not allowing CR.

Using each of the exception patterns from the table above, the regular expression to validate employee payroll NI numbers is (new lines are to fit page only):

([ABEGHJ-LORSW-Z][ABCEGHJ-NPRSTW-Z])|(C[ABCEGHJ-NPSTW-Z])
|(M[ABCEGHJ-NPRSTX-Z])|(N[ABEGHJ-NPRSTW-Z])|(P[ABCEGHJ-NRSTWX])
|(T[ABCEGHJ-MPRSTW-Z])[0-9]{6}[A-D]

From time to time, HMRC publish the list of prefixes in use. This list gradually grows and so if you need this level of accuracy, you would need to be update your regular expression each time HMRC need to increase the list. The following list was supplied in the RTI specification (30th September 2011) - note that it contains some administrative prefixes:

AA, AB, AE, AH, AK, AL, AM, AP, AR, AS, AT, AW, AX, AY, AZ 
BA, BB, BE, BH, BK, BL, BM, BT
CA, CB, CE, CH, CK, CL, CR
EA, EB, EE, EH, EK, EL, EM, EP, ER, ES, ET, EW, EX, EY, EZ 
GY
HA, HB, HE, HH, HK, HL, HM, HP, HR, HS, HT, HW, HX, HY, HZ
JA, JB, JC, JE, JG, JH, JJ, JK, JL, JM, JN, JP, JR, JS, JT, JW, 
JX, JY, JZ
KA, KB, KE, KH, KK, KL, KM, KP, KR, KS, KT, KW, KX, KY, KZ
LA, LB, LE, LH, LK, LL, LM, LP, LR, LS, LT, LW, LX, LY, LZ
MA, MW, MX
NA, NB, NE, NH, NL, NM, NP, NR, NS, NW, NX, NY, NZ
OA, OB, OE, OH, OK, OL, OM, OP, OR, OS, OX
PA, PB, PC, PE, PG, PH, PJ, PK, PL, PM, PN, PP, PR, PS, PT, PW, PX, PY 
RA, RB, RE, RH, RK, RM, RP, RR, RS, RT, RW, RX, RY, RZ
SA, SB, SC, SE, SG, SH, SJ, SK, SL, SM, SN, SP, SR, SS, ST, SW, 
SX, SY ,SZ 
TA, TB, TE, TH, TK, TL, TM, TP, TR, TS, TT, TW, TX, TY, TZ
WA, WB, WE, WK, WL, WM, WP
YA, YB, YE, YH, YK, YL, YM, YP, YR, YS, YT, YW, YX, YY, YZ
ZA, ZB, ZE, ZH, ZK, ZL, ZM, ZP, ZR, ZS, ZT, ZW, ZX, ZY

If you need to be this precise, the regular expression is simpler, but longer (the administrative prefixes removed, new lines added purely to fit the page):

(AA|AB|AE|AH|AK|AL|AM|AP|AR|AS|AT|AW|AX|AY|AZ|BA|BB|BE|BH|BK|BL|BM|BT
|CA|CB|CE|CH|CK|CL|EA|EB|EE|EH|EK|EL|EM|EP|ER|ES|ET|EW|EX|EY|EZ|GY
|HA|HB|HE|HH|HK|HL|HM|HP|HR|HS|HT|HW|HX|HY|HZ
|JA|JB|JC|JE|JG|JH|JJ|JK|JL|JM|JN|JP|JR|JS|JT|JW|JX|JY|JZ
|KA|KB|KE|KH|KK|KL|KM|KP|KR|KS|KT|KW|KX|KY|KZ
|LA|LB|LE|LH|LK|LL|LM|LP|LR|LS|LT|LW|LX|LY|LZ|MA|MX
|NA|NB|NE|NH|NL|NM|NP|NR|NS|NW|NX|NY|NZ
|OA|OB|OE|OH|OK|OL|OM|OP|OR|OS|OX
|PA|PB|PC|PE|PG|PH|PJ|PK|PL|PM|PN|PR|PS|PT|PW|PX
|RA|RB|RE|RH|RK|RM|RP|RR|RS|RT|RW|RX|RY|RZ
|SA|SB|SC|SE|SG|SH|SJ|SK|SL|SM|SN|SP|SR|SS|ST|SW|SX|SY|SZ
|TA|TB|TE|TH|TK|TL|TM|TP|TR|TS|TT|TW|TX|TY|TZ|WA|WB|WE|WK|WL|WM|WP
|YA|YB|YE|YH|YK|YL|YM|YP|YR|YS|YT|YW|YX|YY|YZ
|ZA|ZB|ZE|ZH|ZK|ZL|ZM|ZP|ZR|ZS|ZT|ZW|ZX|ZY)[0-9]{6}[A-D]

It should be noted that this expression would need to be updated with new prefixes as they are introduced by HMRC.

Summary

For simply looking for numbers potentially in NI number format, the following simple expression would suffice:

[A-Z]{2}[0-9]{6}[A-Z]

The key validation for current employee payroll NI Numbers would be:

([ABEGHJ-LORSW-Z][ABCEGHJ-NPRSTW-Z])|(C[ABCEGHJ-NPSTW-Z])
|(M[ABCEGHJ-NPRSTX-Z])|(N[ABEGHJ-NPRSTW-Z])
|(P[ABCEGHJ-NRSTWX])|(T[ABCEGHJ-MPRSTW-Z])[0-9]{6}[A-D]

Try out your regular expressions on the regular expression calculator: Regular Expression Calculator

Regular expression information: Regular Expression Information

PeopleSoft HRMS already has built in UK National Insurance validation, but you can use Regular Expressions in PeopleSoft - PeopleSoft Regular Expressions