1.2.2 • Published 4 years ago

zip-it-gb v1.2.2

Weekly downloads
3
License
ISC
Repository
github
Last release
4 years ago

zip-it

A small library that helps you to verify postal (zip) codes from around the globe

Installation

As a node package:

npm install zip-it --save

And in your code:

const zipit = require('zip-it')

As a bower component:

bower install zip-it --save

And in your page:

<script src="bower_components/zip-it/zip-it.min.js"></script> <!-- of course, you can use zip-it.js if you want to -->

Simple usage

console.log(zipit.verifyCode('US', 37188)); // White house zip code
// => true
console.log(zipit.verifyCode('CA', 'H0H 0H0')); // Santa Claus's postal code
// => true
console.log(zipit.verifyCode('CN', 'Not a valid postal code'));
// => false
console.log(zipit.verifyCode('AA', 'Not a valid country code'));
// => undefined

Full API

zipit.verifyCode(countryCode, postalCode)

Returns true if postalCode is a valid postal code in the country, false if it's invalid or undefined if countryCode is not a valid country code

Notes:

  • The verification is not done on the exact address, but verifies postalCode adheres to the format of the postal code in the given country.
  • Many countries (66 to be exact) don't have a postal code of any sort. In that case, any value of postalCode will return true.

Arguments

  1. countryCode (String) – a two capital letters string, representing the relevant country according to iso 3166-2
  2. postalCode (String|Number) – the postal code to verify

zipit.isCountrySupported(countryCode)

Returns true if countryCode is supported, false otherwise. Currently zip-it suppports all 234 countries I was able to find in Wikipedia, so if isCountrySupported returns false, there's a good chance the code is invalid.

Arguments

  1. countryCode (String) – a two capital letters string, representing the relevant country according to iso 3166-2

zipit.supportedCountryCodes()

Return all supported country codes


zipit.getRegexForCountry(countryCode)

Returns the regular expression used for that country. Can be useful in a pattern attribute of an <input> tag

Arguments

  1. countryCode (String) – a two capital letters string, representing the relevant country according to iso 3166-2