uk-postcode-validator

Validate UK postcodes.

Usage no npm install needed!

<script type="module">
  import ukPostcodeValidator from 'https://cdn.skypack.dev/uk-postcode-validator';
</script>

README

uk-postcode-validator npm size Build Status

Validate UK postcodes.

Installation

$ npm install uk-postcode-validator

Example

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

API

isValid(input)

Returns the true or false based on the postcode validity.

input

Type: string

Logic

    "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.

Inspired by

This stack overflow discussion and this gov.uk document.