An open API service indexing awesome lists of open source software.

https://github.com/peburrows/validates_as_postal_code

Rails plugin to validate postal codes from around the world. validates_as_postal_code
https://github.com/peburrows/validates_as_postal_code

Last synced: 12 months ago
JSON representation

Rails plugin to validate postal codes from around the world. validates_as_postal_code

Awesome Lists containing this project

README

          

## Usage

### validates\_as\_postal\_code



# when :country parameter is a String,
# validate as a postal code from a specific country
validates_as_postal_code :postal_code, :country => 'United States'

# when :country parameter is a Symbol
# validate depending on the value of a specific field in the model
validates_as_postal_code :postal_code, :country => :cardholder_country

### Country Lists

Each of the following return a list of countries with the following information for each country:
*name*, *region*, *continent*, *ISO numeric code*, *ISO 2-letter code*, *ISO 3-letter code*, *postal code regular expression*



VST::Validations::Addresses.north_america
# => [
# { :name => 'Canada',
# :numeric => '124',
# :alpha2 => 'CA',
# :alpha3 => 'CAN',
# :continent => 'North America',
# :region => 'North America',
# :postal_code => /^([A-Z]\d[A-Z][\-\s]?\d[A-Z](\d)?)$/i
# },
# { :name => 'United States',
# :numeric => '840',
# :alpha2 => 'US',
# :alpha3 => 'USA',
# :continent => 'North America',
# :region => 'North America',
# :postal_code => /^\d{5}([\-\s]?\d{4})?$/
# }
# ]

VST::Validations::Addresses.eu
# => same type of info for the 27 European Union countries

### find\_by\_* & find\_all\_by\_*

The VST::Validations::Addresses module also provides *find\_by\_\** (for a single country) and *find\_all\_by\_\** (for a list of countries)


VST::Validations::Addresses.north_america
# -- is the same as --
VST::Validations::Addresses.find_all_by_continent('North America')

VST::Validations::Addresses.find_by_iso_code('840')
# => { :name => 'United States',
# :numeric => '840',
# :alpha2 => 'US',
# :alpha3 => 'USA',
# :continent => 'North America',
# :region => 'North America',
# :postal_code => /^\d{5}([\-\s]?\d{4})?$/
# }

## Caveats

Currenty supports the US, Canada and countries from the EU.
Also, **this is very much beta and things may change**