Reference · British country module
isValidUKPostcodefunction
Whether a postcode follows Royal Mail's grammar: one of the six shapes (M2 5BQ, M34 4AB, CR0 2YR, DN16 9AA,
W1A 4ZZ, EC1A 1HQ) with the letters each place allows, or GIR 0AA. Letter case and the space do not matter.
Whether the postcode is in use is a different question; parseUKPostcode and the validator also check its area
and district.
Import it
import { isValidUKPostcode } from "@johnmorrisdotca/address-plus/gb";
Definition
function isValidUKPostcode(postcode: string): boolean
Parameters
| Name | Type | Description |
|---|---|---|
postcode | string | The postcode. |
Returns
boolean
true when it has a postcode's shape and letters.
Examples
import { isValidUKPostcode } from "@johnmorrisdotca/address-plus/gb";
console.log(["EC1A 1BB", "sw1a1aa", "GIR 0AA", "Q1 1AA", "M5V 1A1"].map(isValidUKPostcode));
// → [true,true,true,false,false][ true, true, true, false, false ]What it prints (run when this page was built)