ico-validator
Ruby validator for format of Czech company identification number - ICO
Contains:
- Standalone ruby validator
- Rails ActiveModel::EachValidator
- JS validator
What is validated:
- ICO length - must be exactly 8 characters
- ICO must contain only numbers
- 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.