Project

postoffice

0.01
No commit activity in last 3 years
No release in over 3 years
Implements an ActiveModel::EachValidator that can be added to your models in a single line, to validate the format of various world postal codes. More features for working with addresses to come.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Postoffice

Rails gem to validate format of various world postal and zip codes, and more.

Installation:

gem install postoffice

or add the following to your Gemfile

gem 'postoffice'

Usage as a Rails validator:

To validate various world postal codes:

class MyClass < ActiveRecord::Base
  validates :postcode, :postcode_format => {:country_code => :us, :message => "entered is not a valid postcode for #{PostOffice.country_name(options[:country_code])}."}
end

To validate Canadian postal codes, simply change the symbol :us to :ca, and you'll get: An example Postoffice error message

Note: Don't forget to use the ':allow_blank => true' option, when needed.

Country name codes abide by the ISO 3166-1 alpha-2 standard. 155 countries are supported: see COUNTRYCODES.md for the full list.

General usage:

Validating correctness of postcodes directly:

# Validating an English postcode. Returns 0, since this postcode is valid, returns nil otherwise
PostOffice.validate_postcode('N1 9GU', :gb)

To produce a string of the name of a country, input a supported 2-digit country code as a symbol.

Example:

PostOffice.country_name(:us)

This will output "United States".

PostOffice.country_name(:lu)

This will output "Luxembourg".

License:

See the LICENSE file.