Sha256: efd45b6b8adda09af0bcb4c46c3ee00f5bfe03be25b484afbc4a4c1392d198ff

Contents?: true

Size: 820 Bytes

Versions: 4

Compression:

Stored size: 820 Bytes

Contents

require 'active_model'

class IbanValidator < ActiveModel::EachValidator

  def validate_each(record, attribute, value)
    record_error(record, attribute, value) unless value =~ regexp && valid_iban?(value)
  end
  
  private
  
  def record_error(record, attribute, value)
    record.errors.add(attribute, :invalid_iban)
  end
  
  def regexp
    /[A-Z]{2}[a-zA-Z0-9]{14,}/
  end

  def valid_iban?(iban)
    # Move first four characters to end of string
    dummy_iban = iban.slice(4..-1) + iban.slice(0..3)

    # Substitute all letters with integers
    dummy_iban.split(//).each { |char| dummy_iban.gsub!(char, (char.downcase.ord - 87).to_s) if (char =~ /[a-zA-Z]/).present? }

    # Check if division by 97 yields a remainder of 1, in which case it could be a valid IBAN
    (dummy_iban.to_i % 97) == 1
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
bank-validator-0.2.1 lib/active_model/iban_validator.rb
bank-validator-0.2.0 lib/active_model/iban_validator.rb
bank-validator-0.1.0 lib/active_model/iban_validator.rb
bank-validator-0.0.6 lib/active_model/iban_validator.rb