ico-validator

Build Status Dependency Status Code Climate Test Coverage

Ruby validator for format of Czech company identification number - ICO

Contains:

  1. Standalone ruby validator
  2. Rails ActiveModel::EachValidator
  3. JS validator

What is validated:

  1. ICO length - must be exactly 8 characters
  2. ICO must contain only numbers
  3. ICO number must satisfy last digit control formula

Usage

Ruby validator

Ruby validator is one simple module with only one public method IcoValidation.valid_ico? which returns true or false.

require 'ico_validator'

IcoValidation.valid_ico?('12345678') # invalid ico
=> false

IcoValidation.valid_ico?('27169278')
=> true

Rails validator

Rails validator is implemented as ActiveModel::EachValidator. So it's easy to use within your AR models:

class Company < ActiveRecord::Base
  validates :ico, ico: true
end

If validation doesn't pass it adds :invalid_format error on validated attribute.

JS validation

ico-validator is Rails engine, so if you want to validate ico with javascript just add following to application.js

//= require ico-validator

And then on any field you want to be validated with JS:

<%= form_for(...) do |f| %>
  <%= f.text_field :ico, class: 'ico-validate' %>
<% end %>

jQuery will bind validations there.