uk-postcode-validator
v2.0.0
Published
Validate UK postcodes.
Maintainers
Readme
uk-postcode-validator
Check whether a string is a correctly formatted UK postcode.
- Checks every position. Each position only accepts the letters used in real postcodes, so
Q1 1AAorN7 7CAare rejected. - Forgiving with input. Case-insensitive, with or without the space, and surrounding whitespace is ignored.
- Tiny. One function, no dependencies.
- Typed. TypeScript declarations included.
Install
npm install uk-postcode-validatorUsage
import { isValid } from "uk-postcode-validator";
// or const { isValid } = require("uk-postcode-validator");
isValid("N7 7AJ"); // true
isValid("n77aj"); // true
isValid(" EC1A 1BB "); // true
isValid("GIR 0AA"); // true
isValid("N7 A7J"); // false
isValid("Q1 1AA"); // false
isValid("90210"); // falseAPI
| Import | Value |
| ------------------------------------ | --------------------------------------------------------- |
| isValid(postcode: string): boolean | true if postcode is a correctly formatted UK postcode |
| default | The same isValid function |
Validation rules
A postcode is an outward code followed by an inward code, optionally separated by whitespace. GIR 0AA is also accepted.
Outward code, one of: A9 A99 AA9 AA99 A9A AA9A
Inward code: 9AA
1st position: any letter except Q, V, X
2nd position: any letter except I, J, Z
3rd position: (A9A) only A, B, C, D, E, F, G, H, J, K, P, S, T, U, W
4th position: (AA9A) only A, B, E, H, M, N, P, R, V, W, X, Y
Inward code: letters except C, I, K, M, O, VIt checks the format only, not whether the postcode exists. For that, look it up against a dataset such as the ONS Postcode Directory.
Not included: BFPO addresses, overseas territories (e.g. ASCN 1ZZ, KY1-1001) and pseudo-postcodes such as ZZ99 9ZZ.
Migrating from 1.x
- Validation is stricter. Text around a postcode (
"GIR0AA nonsense") and letters not used in a given position ("Q1 1AA","ZZ99 9ZZ") are now rejected. - With
require, use the named export:const { isValid } = require("uk-postcode-validator")instead of.default. The default export still works. - The build targets ES2017, so very old browsers need it transpiled.
Contributing
pnpm install
pnpm testCredits
Validation rules from BS 7666, as summarised in Postcodes in the United Kingdom on Wikipedia.
License
MIT © Luca Lischetti
