National IDs Validator for Ruby on Rails (Active Model)
It provides validators for national identification numbers. Currently the following countries are supported:
- Norway (NO) - Fødselsnummer
- Poland (PL) - PESEL
Usage
Add to your Gemfile:
gem 'national-ids-validator'
Run:
bundle install
Then add the following to your model:
require 'national_ids_validator'
validates :personal_id_attribute, :national_id => {country: "PL"}
Options
A custom error message can be provided:
require 'national_ids_validator'
validates :personal_id_attribute, :national_id => {country: "PL", message: "is not valid personal number"}
If the nil value should be allowed, it need to be explicitely stated:
require 'national_ids_validator'
validates :personal_id_attribute, :national_id => {country: "PL", allow_nil: true}
Retrieving data out of the personal number
Often personal number contains useful data like birth date and/or gender. You may use the following commands to retrieve them:
- gender
require 'national_ids_validator'
NationalIdsValidator.new("12030599592", "NO").gender %returns 0
It will return 0 for man, 1 for woman and nil for invalid number.
Contributing
- 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 new Pull Request
Credit
Based on https://github.com/balexand/email_validator and https://github.com/max-power/iban.
License
MIT License. Copyright 2009-2014 Grzegorz Brzezinka. http://brzezinka.eu