Add zipcode validation support to Rails (ActiveModel), considering different zipcode country formats.
ValidatesZipcode
currently support 159 countries. Regex data taken from several sources, mainly from the CLDR database. Any other country's zipcode will validate without errors.
ValidatesZipcode
works for Rails 3 & 4 and Ruby >= 1.9.3.
Add this line to your application's Gemfile:
gem 'validates_zipcode'
And then execute:
$ bundle
Or install it yourself as:
$ gem install validates_zipcode
validates_zipcode :zipcode
validates :zipcode, zipcode: true
ValidatesZipcode
expects the model to have an attribute called country_alpha2
to contain the country code.
You can provide your own country_code using :country_code
option, or specify which attribute contains this information
using :country_code_attribute
option.
validates :zipcode, zipcode: { country_code: :es }
validates :zipcode, zipcode: { country_code_attribute: :my_country_code_column }
If you need to localize the error message, just add this to your I18n locale file:
errors:
messages:
invalid_zipcode: Your zipcode error message.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
To see the generous people who have contributed code, take a look at the contributors list.
Copyright (c) 2014 David Gil Pérez, released under the MIT license