Giter Site home page Giter Site logo

postal-codes-js's Introduction

Build Status Coverage Status npm version

postal-codes.js

Provides javascript postal code validation for all countries. Supports both Node.js and web browser usage.

Validation rules

  1. Characters " " (space) and "-" (dash) are removed from the input string.
  2. Input is case insensitive.
  3. Supports both ISO 3166-1 alpha-2 and ISO 3166-1 alpha-3 country codes.
  4. Validates optional n-digit extention seperated by a space or hyphen.

Testing with mocha

$ npm test
$ npm run coverage

Usage

const postalCodes = require('postal-codes-js');
const countryCode = 'CH'; // ISO 3166-1 alpha-2 or alpha-3 country code as string.
const postalCode = '8008'; // Postal code as string or number.
postalCodes.validate(countryCode, postalCode); // Returns 'true' if valid, error message as string otherwise.

// All the calls below returns true
postalCodes.validate('bg', '1003');
postalCodes.validate('gb', 'EC1A 1BB');
postalCodes.validate('GB', 'EC1A 1BB');
postalCodes.validate('GBR', 'EC1A 1BB');
postalCodes.validate('gb', 'EC1A1BB');
postalCodes.validate('gb', 'EC1A-1BB');
postalCodes.validate('tr', '33150');
postalCodes.validate('TR', '33150');
postalCodes.validate('TUR', '33150');
postalCodes.validate('us', '22313');
postalCodes.validate('USA', '91746-2302');

// All the calls below return a string
postalCodes.validate('UK', 'EC1A 1BB');
 > Unknown alpha2/alpha3 country code: UK

postalCodes.validate('PL', '9999');
 > Postal code 9999 is not valid for country PL

postalCodes.validate('CH');
 > Missing postal code.

Contribution

Contributions are more than welcome, just fork the repo and create a pull-request ;)

Contact

[email protected]

postal-codes-js's People

Contributors

indirap avatar istanishev avatar mkacmar avatar monicbhanushali avatar ozankaya avatar rnowosielski avatar tomhooijenga avatar willfarrell avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.