NifVal

Description

NifVal is a simple gem which adds a Spanish NIF/NIE/CIF validator to ActiveModel. You simply have to validate ":nif" for "true" for a specific field.

Installation

Simply add this gem to your Gemfile:

gem "nifval"

and execute bundle.

Note: It appears that if we load NifVal after Devise, Nifval won't work. In order to make everything work, simply add Nifval before Devise in the Gemfile.

Then run the generator in order to install the I18n template:

rails generate nifval:install

If you want to validate a Nif via Javascript, you can also add:

rails generate nifval:javascript

And you are done!

Usage

On your model

Let's see an example:

class Person
  validates :nif, :nif => true
  attr_accessor :nif
end

Then if we create an instance and see if it is valid, the validation will be checked.

Let's see one example for each case. A successful validation (correct control digit T for 00000000):

p = Person.new("00000000T")
p.valid? # will return true

And an unsuccessful one (mistaken control digit C for A2345678):

p = Person.new("A2345678C")
p.valid? # will return false

 Via Javascript

Please refer to the Installation section on how to get Nifval's Javascript file.

You only have to add nifval.js to your application layout. Assuming the use of HAML:

= javascript_include_tag "nifval"

And then execute the nifval(nifToCheck) function, which returns a boolean.

nifval("00000000T") // returns true
nifval("A2345678C") // returns false

Example

You can see both situations via the test application included in this gem. This test app is also available at http://nifvalapp.heroku.com.

Documentation

Adapted from here.

You can also find more information on Wikipedia.