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
- Host: GitHub
- URL: https://github.com/peburrows/validates_as_postal_code
- Owner: peburrows
- Created: 2009-01-09T16:51:48.000Z (over 17 years ago)
- Default Branch: master
- Last Pushed: 2010-11-26T07:43:46.000Z (over 15 years ago)
- Last Synced: 2025-06-07T11:08:50.268Z (about 1 year ago)
- Language: Ruby
- Homepage:
- Size: 113 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.markdown
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**