Class: BankAccountValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Extended by:
ActiveSupport::Concern
Defined in:
lib/elfproef/bank_account_validator.rb

Overview

Validates if the specified value is a valid bank account number using the 11-test

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.validate_account_number(value, options = {}) ⇒ Object

Takes the bank account number and returns true if:

* The number is 1...7 digit ING account number (no verification is possible)
* Is 9 or 10 digits and passes the 11-test


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/elfproef/bank_account_validator.rb', line 20

def self.(value, options = {})
  number = value.to_s.gsub(/\D/, '').strip
  # Not valid if length is 0, 8 or > 10
  return false if number.length == 0 || number.length == 8 || (value.length > 10 && value.length < 15) || value.length > 31

  # ING account numbers
  return true if (1..7).include?(number.length)

  # Validate length 9 and 10 numbers as bank accounts
  return true if self.validate_with_eleven_test(number) if (number.length == 9 || number.length == 10)

  # If all other validations are failing, we are possibly dealing with an IBAN number
  return true if self.validate_with_iban_test(value)

  return false
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



9
10
11
12
13
# File 'lib/elfproef/bank_account_validator.rb', line 9

def validate_each(record, attribute, value)
  unless self.class.(value, options)
    record.errors.add(attribute, :invalid_bank_account, options)
  end
end