Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression to validate UK National Insurance Number

Tags:

I have the following regular expression which validates the British National Insurance Number

^([a-zA-Z]){2}( )?([0-9]){2}( )?([0-9]){2}( )?([0-9]){2}( )?([a-zA-Z]){1}?$ 

Accepted values are:

AB 12 34 56 A  AB123456A 

I want this values also to be accepted. can anyone please help me sort out this?

AB 123456 A  AB 123 456 A  AB 1 2345 6 A      AB   12   34   56 A    (multiple space anywhere) 

The RE should work even if there are extra or no spaces in the string. Is this possible to do in RE? Thank you in advance.

like image 895
Ishan Avatar asked Apr 18 '12 06:04

Ishan


People also ask

What letters show your National Insurance number?

Your National Insurance number can normally be found on a letter from the Department for Work and Pensions (DWP) or on your bank statement if your pension is paid into your bank account. Your National Insurance number is 9 digits long and starts with two letters, followed by six numbers and one letter e.g. AB123456C.

How long is the UK National Insurance number valid for?

You can start work before you receive your National Insurance number if you can prove you have the right to work in the UK. If you already have a National Insurance number, you do not need to apply for a new one, even if your personal details change. Your National Insurance number remains the same for life.

How does a British citizen get a National Insurance number?

If you live in the UK you'll normally receive a National Insurance number automatically at age 16 if a parent has filled in a Child Benefit claim form for you.


2 Answers

Actually, NIN doesn't allow D, F, I, Q, U or V for the first two letters and doesn't allow O for the second letter; on top of this, the prefix letters can not be BG, GB, NK, KN, TN, NT and ZZ. Also the suffix letter is either A, B, C or D, but may be represented by a space if unknown. - http://en.wikipedia.org/wiki/National_Insurance_number#Format

As such, a more valid check would be (I have only supplied a version with capital letters, can easily be altered for lower-case):

^(?!BG)(?!GB)(?!NK)(?!KN)(?!TN)(?!NT)(?!ZZ)(?:[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z])(?:\s*\d\s*){6}([A-D]|\s)$ 
like image 198
Andrew Bauer Avatar answered Sep 24 '22 03:09

Andrew Bauer


Edit: Andrew Bauer modified my answer to add checks for allowed/disallowed characters that were unknown at the time I answered. You should up-vote his answer since it is more complete and apparently performs better validation.


If you can't just remove all the whitespace first, this should work:

^\s*[a-zA-Z]{2}(?:\s*\d\s*){6}[a-zA-Z]?\s*$ 

Explanation:

^                 # beginning of string \s*               # optional leading whitespace [a-zA-Z]{2}       # match two letters (?:\s*\d\s*){6}   # six digits, with optional whitespace leading/trailing [a-zA-Z]?         # zero or one letter \s*               # optional trailing whitespace (just in case) $                 # end of string 
like image 35
alan Avatar answered Sep 22 '22 03:09

alan