niho/personal-number/1.2.1

PersonalNumber.UK

Parse UK National Insurance Numbers.

See https://www.gov.uk/hmrc-internal-manuals/national-insurance-manual/nim39110 for specification, and see https://design-system.service.gov.uk/patterns/national-insurance-numbers/ for designing your UI.

In particular:

  • When asking for a National Insurance Number:
    • allow for 13 characters as National Insurance Numbers are spaced in pairs followed by a single letter
    • let users enter upper and lower case letters, additional spaces and punctuation
    • ignore any unwanted characters before validating
    • avoid using ‘AB 12 34 56 C’ as an example because it belongs to a real person and use ‘QQ 12 34 56 C’ instead
    • set the spellcheck attribute to false so that browsers do not spellcheck the National Insurance Number

Definition

An opaque type representing a valid national insurance number.

Errors

= InvalidLength
| InvalidFormat FormatError
| InvalidPrefix PrefixError
| InvalidSuffix

If the parsing fails an error of this type is returned.

A National Insurance Number is made up of a 2 letter prefix, 6 numbers and a final letter, which is always A, B, C, or D.

It looks something like this:

QQ 12 34 56 A

All prefixes are valid except:

  • The characters D, F, I, Q, U, and V are not used as either the first or second letter of a prefix.
  • The letter O is not used as the second letter of a prefix.
  • Prefixes BG, GB, KN, NK, NT, TN and ZZ are not to be used

You could turn these errors into strings something like this:

errorToString : ValidationError -> String
errorToString e =
    case e of
        InvalidLength ->
            "National Insurance Numbers should have 9 characters, like  QQ 12 34 56 C  or  QQ123456C."


        InvalidFormat DoesNotStartWithTwoLetters ->
            "National Insurance Numbers should start with two letters, like  QQ 12 34 56 C  or  QQ123456C."


        InvalidFormat DoesNotEndWithALetter ->
            "National Insurance Numbers should end with a single letter A, B, C or D, like  QQ 12 34 56 C  or  QQ123456C."


        InvalidFormat DoesNotHaveSixNumbersInTheMiddle ->
            "National Insurance Numbers should have six digits in the middle, like  QQ 12 34 56 C  or  QQ123456C."


        InvalidPrefix DisallowedPrefixLetter ->
            "D, F, I, Q, U, and V are not allowed in the first two letters of National Insurance Numbers."


        InvalidPrefix DisallowedSecondLetterO ->
            "O is not allowed in the second letter of National Insurance Numbers."


        InvalidPrefix DisallowedPrefix ->
            "BG, GB, KN, NK, NT, TN and ZZ are not allowed at the start of National Insurance Numbers."


        InvalidSuffix ->
            "The last letter of National Insurance Numbers can only be A, B, C or D."

Strings

Parse a string into a National Insurance Number.

Converts a national insurance number to string representation in the short format that is commonly used for database storage (AB123456C).

Format a national insurance number in the long format that is commonly used for human communication (AB 12 34 56 C).

JSON

Decode a National Insurance Number.

Encode a national insurance number into a JSON value.