Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including postcode with all npm packages installed. Try it out:

const Postcode = require("postcode"); const postcode = new Postcode("ec1v9lb"); postcode.valid(); postcode.normalise(); postcode.outcode(); postcode.incode(); postcode.area(); postcode.district(); postcode.subDistrict(); postcode.sector(); postcode.unit();

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

postcode v2.0.0

UK Postcode helper methods

Postcode.js

Validate & parse UK postcodes

CircleCI Coverage Status Dependencies Size Downloads Try postcode on RunKit

Utility methods for UK Postcodes, including validating the shape of a postcode, extracting postcode elements (like incodes, outcodes, areas and more).

Tested against ~1.7 million postcodes on ONSPD.

Features

Links

Getting Started

Installation

npm install postcode

Wield

import Postcode from "postcode";

Validate

Postcode.isValid("AA1 1AB"); // => true

Parse

Pass a string to Postcode.parse. This will return a valid or invalid postcode instance which can be easily destructured.

Valid Postcode

const {
  normalised,  // => "SW1A 2AA"
  outcode,     // => "SW1A"
  incode,      // => "2AA"
  area,        // => "SW"
  district,    // => "SW1"
  unit,        // => "AA"
  sector,      // => "SW1A 2"
  subDistrict, // => "SW1A"
  valid,       // => true
} = Postcode.parse("Sw1A     2aa");

Invalid postcode

const {
  normalised,  // => null
  outcode,     // => null
  incode,      // => null
  area,        // => null
  district,    // => null
  unit,        // => null
  sector,      // => null
  subDistrict, // => null
  valid,       // => false
} = Postcode.parse("    Oh no, ):   ");

Accessor Overview

Postcode.outcode.incode.area.district.subDistrict.sector.unit
AA9A 9AAAA9A9AAAAAA9AA9AAA9A 9AA
A9A 9AAA9A9AAAA9A9AA9A 9AA
A9 9AAA99AAAA9nullA9 9AA
A99 9AAA999AAAA99nullA99 9AA
AA9 9AAAA99AAAAAA9nullAA9 9AA
AA99 9AAAA999AAAAAA99nullAA99 9AA

Static Methods

If you're just after a single value, you would be better served by calling a static method on Postcode.

Postcode.isValid("Sw1A 2aa");      // => true

Postcode.toNormalised("Sw1A 2aa");  // => "SW1A 2AA"
Postcode.toOutcode("Sw1A 2aa");     // => "SW1A"
Postcode.toIncode("Sw1A 2aa");      // => "2AA"
Postcode.toArea("Sw1A 2aa");        // => "AA"
Postcode.toDistrict("Sw1A 2aa");    // => "SW1"
Postcode.toSubDistrict("Sw1A 2aa"); // => "SW1A"
Postcode.toSector("Sw1A 2aa");      // => "SW1A 2"
Postcode.toUnit("Sw1A 2aa");        // => "AA"

Older API

Below documents the old validation API, which continues to be supported.

Create an instance of Postcode to perform utility methods, like so:

const Postcode = require("postcode");

const postcode = new Postcode("ec1v9lb");

Perform simple validations, parsing and normalisation

postcode.valid()        // => true
postcode.normalise()    // => "EC1V 9LB"

postcode.outcode()      // => "EC1V"
postcode.incode()       // => "9LB"
postcode.area()         // => "EC"
postcode.district()     // => "EC1"
postcode.subDistrict()  // => "EC1V"
postcode.sector()       // => "EC1V 9"
postcode.unit()         // => "LB"

Method Overview

Postcode.outcode().incode().area().district().subDistrict().sector().unit()
AA9A 9AAAA9A9AAAAAA9AA9AAA9A 9AA
A9A 9AAA9A9AAAA9A9AA9A 9AA
A9 9AAA99AAAA9nullA9 9AA
A99 9AAA999AAAA99nullA99 9AA
AA9 9AAAA99AAAAAA9nullAA9 9AA
AA99 9AAAA999AAAAAA99nullAA99 9AA

Misc. Class Methods include

Postcode.validOutcode(outcode)

Definitions

Outcode

The outward code is the part of the postcode before the single space in the middle. It is between two and four characters long. A few outward codes are non-geographic, not divulging where mail is to be sent. Examples of outward codes include "L1", "W1A", "RH1", "RH10" or "SE1P".

Incode

The inward part is the part of the postcode after the single space in the middle. It is three characters long. The inward code assists in the delivery of post within a postal district. Examples of inward codes include "0NY", "7GZ", "7HF", or "8JQ".

Area

The postcode area is part of the outward code. The postcode area is between one and two characters long and is all letters. Examples of postcode areas include "L" for Liverpool, "RH" for Redhill and "EH" Edinburgh. A postal area may cover a wide area, for example "RH" covers north Sussex, (which has little to do with Redhill historically apart from the railway links), and "BT" (Belfast) covers the whole of Northern Ireland.

District

The district code is part of the outward code. It is between two and four characters long. It does not include the trailing letter found in some outcodes. Examples of district codes include "L1", "W1", "RH1", "RH10" or "SE1".

Sub-District

The sub-district code is part of the outward code. It is often not present, only existing in particularly high density London districts. It is between three and four characters long. It does include the trailing letter omitted from the district. Examples of sub-district codes include "W1A", "EC1A", "NW1W", "E1W" or "SE1P".

Note: for outcodes not ending with a letter, subDistrict will return null. For example:

new Postcode("SW1A 1AA").subDistrict()  // => SW1A
new Postcode("E1W 1LD").subDistrict()   // => E1W
new Postcode("PO16 7GZ").subDistrict()  // => null
new Postcode("B5 5NY").subDistrict()    // => null

Sector

The postcode sector is made up of the postcode district, the single space, and the first character of the inward code. It is between four and six characters long (including the single space). Examples of postcode sectors include "SW1W 0", "PO16 7", "GU16 7", or "L1 8", "CV1 4".

Unit

The postcode unit is two characters added to the end of the postcode sector. Each postcode unit generally represents a street, part of a street, a single address, a group of properties, a single property, a sub-section of the property, an individual organisation or (for instance Driver and Vehicle Licensing Agency) a subsection of the organisation. The level of discrimination is often based on the amount of mail received by the premises or business. Examples of postcode units include "NY" (from "SW1W 0NY"), "GZ" (from "PO16 7GZ"), "HF" (from "GU16 7HF"), or "JQ" (from "L1 8JQ").

Note on Postcode Validation

Postcodes cannot be validated just with a regular expression. Proper postcode validation requires having a full list of postcodes to check against. Relying on a regex will produce false postives/negatives.

A complete list of Postcodes can be obtained from the ONS Postcode Directory, which is updated every 3 months.

Testing

npm test

License

MIT

Contains Ordnance Survey Data © Crown Copyright & Database Right

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free