Validate UK postcodes.
npm install uk-postcode-validator
import isValid from "uk-postcode-validator";
// or const isValid = require("uk-postcode-validator").default;
isValid("N7 7AJ");
//=> true
isValid("N77AJ");
//=> true
isValid("GIR 0AA");
//=> true
isValid("N7 A7J");
//=> false
isValid("90210");
//=> false
Returns the true
or false
based on the postcode validity.
Type: string
"GIR 0AA"
OR
One letter followed by either one or two numbers
OR
One letter followed by a second letter that must be one of ABCDEFGHJKLMNOPQRSTUVWXY (i.e..not I) and then followed by either one or two numbers
OR
One letter followed by one number and then another letter
OR
A two part post code where the first part must be One letter followed by a second letter that must be one of ABCDEFGHJKLMNOPQRSTUVWXY (i.e..not I) and then followed by one number and optionally a further letter after that
AND
The second part must be One number followed by two letters.
A combination of upper and lower case characters is allowed.
The length is determined by the regular expression and is between 2 and 8 characters.
This stack overflow discussion and this gov.uk document.