NI Number Validation
From TekiWiki
This whole wiki has now moved to http://www.springboardsolutions.com/wiki
Go to this page NI Number Validation
| Line 6: | Line 6: | ||
The basic format is AA999999A, and a check for this format would be: | The basic format is AA999999A, and a check for this format would be: | ||
| - | + | <pre> | |
[A-Z]{2}[0-9]{6}[A-Z] | [A-Z]{2}[0-9]{6}[A-Z] | ||
| - | + | </pre> | |
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. | 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. | ||
| Line 19: | Line 19: | ||
The first letter cannot be D, F, I, Q, U or V. | 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. | The second letter cannot be D, F, I, O, Q, U and V. | ||
| - | + | <pre> | |
[ABCEGHJ-PR-TW-Z][ABCEGHJ-NPR-TW-Z][0-9]{6}[A-D] | [ABCEGHJ-PR-TW-Z][ABCEGHJ-NPR-TW-Z][0-9]{6}[A-D] | ||
| - | + | </pre> | |
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: | 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: | ||
| Line 82: | Line 82: | ||
Using each of the exception patterns from the table above, the regular expression to validate employee payroll NI numbers is: | Using each of the exception patterns from the table above, the regular expression to validate employee payroll NI numbers is: | ||
| - | + | <pre> | |
([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] | ([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] | ||
| - | + | </pre> | |
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: | 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: | ||
| - | + | <pre> | |
AA, AB, AE, AH, AK, AL, AM, AP, AR, AS, AT, AW, AX, AY, AZ | AA, AB, AE, AH, AK, AL, AM, AP, AR, AS, AT, AW, AX, AY, AZ | ||
BA, BB, BE, BH, BK, BL, BM, BT | BA, BB, BE, BH, BK, BL, BM, BT | ||
| Line 107: | Line 107: | ||
YA, YB, YE, YH, YK, YL, YM, YP, YR, YS, YT, YW, YX, YY, YZ | 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 | ZA, ZB, ZE, ZH, ZK, ZL, ZM, ZP, ZR, ZS, ZT, ZW, ZX, ZY | ||
| - | + | </pre> | |
If you need to be this precise, the regular expression is simpler, but longer (the administrative prefixes removed): | If you need to be this precise, the regular expression is simpler, but longer (the administrative prefixes removed): | ||
| - | + | <pre> | |
(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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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] | ||
| - | + | </pre> | |
| - | Summary | + | ==Summary== |
For simply looking for numbers potentially in NI number format, the following simple expression would suffice: | For simply looking for numbers potentially in NI number format, the following simple expression would suffice: | ||
| - | + | <pre> | |
[A-Z]{2}[0-9]{6}[A-Z] | [A-Z]{2}[0-9]{6}[A-Z] | ||
| - | + | </pre> | |
The key validation for current employee payroll NI Numbers would be: | The key validation for current employee payroll NI Numbers would be: | ||
| - | + | <pre> | |
([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] | ([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] | ||
| - | + | </pre> | |
A slightly longer, but more exact expression would be: | A slightly longer, but more exact expression would be: | ||
| - | + | <pre> | |
(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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W|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] | ||
| - | + | </pre> | |
It should be noted that this expression would need to be updated with new prefixes as they are introduced by HMRC. | It should be noted that this expression would need to be updated with new prefixes as they are introduced by HMRC. | ||