Validate & Format Postcodes In Vanilla JavaScript

Category: Form , Javascript | September 18, 2023
Views Total:34 views
Official Page:Go to website
Last Update:September 18, 2023


Validate & Format Postcodes In Vanilla JavaScript


postcode-format is a tiny and fast JavaScript library created for validating and formatting postcodes. Whether you call it a postcode, zip code, or postal code, ‘postcode-format’ ensures it’s in the right format for its respective country.

It takes an ISO country code and postcode as input and returns a formatted postcode if valid or throws an exception if invalid.

See Also:

How to use it:

1. Install and import the postcode-format into your project.

$ npm i postcode-format
import postcodeFormat from 'postcode-format';

2. By feeding in the country’s ISO 3166 code and the postcode you wish to validate, the library quickly returns a formatted version or throws an exception if there are discrepancies.

// => EC1A 1BB
console.log(postcodeFormat('GB', 'EC1A1BB'));
// => 33701-4313
console.log(postcodeFormat('US', '337014313'));
// => 811-5113
console.log(postcodeFormat('jp', '8115113'));

You Might Be Interested In:

Leave a Reply