Skip to content

peburrows/validates_as_postal_code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages