Ruby on Rails模型文件中的IBAN验证

tha*_*y_3 1 ruby validation ruby-on-rails iban

我正在尝试像任何国家的iban代码进行iban验证.我从stackoverflow获得了一些帮助来构建代码,但我仍有一些问题,我不知道它在哪里.

我总是得到'这不是一个有效的IBAN'错误消息.但有时我尝试将iban代码更正为定义的国家/地区.

有没有人帮助我做这个验证的代码,拜托?

代码在这里:

  class BankAccount < ActiveRecord::Base
  belongs_to :user

  validates :bank_name, presence: true
  validate :iban, :valid_iban?, presence: true

  private

    def valid_iban?
            ibans = iban.upcase.scan(/\w/).join

            ibans = ibans.gsub(/_/, '')

            iban_length = ibans.length

            country = ibans.scan(/\A../).join

            length_correct_for_country = true

            case country
                when "IE"
                    if iban_length == 22
                        length_correct_for_country = true
                    else
                        length_correct_for_country = false      
                    end
                when "AL"
                    if iban_length == 28
                        length_correct_for_country = true
                    else
                        length_correct_for_country = false      
                    end
                when "TR"
                    if iban_length == 26
                        length_correct_for_country = true
                    else
                        length_correct_for_country = false      
                    end
                when "GB"
                    if iban_length == 22
                        length_correct_for_country = true
                    else
                        length_correct_for_country = false      
                    end
                when "VG"
                    if iban_length == 24
                        length_correct_for_country = true
                    else
                        length_correct_for_country = false      
                    end
            end

            first_four_characters = ibans.slice!(0..3)

            reordered_number = ibans + first_four_characters

            letters_removed = []
            reordered_number.scan(/./) do |character|
                case character
                when "A"
                    letters_removed << 10
                when "9"
                    letters_removed <<9
                end
            end

            letters_removed = letters_removed.join.to_i

            remainder = letters_removed % 97

            if remainder == 1 && length_correct_for_country

            else
                remainder = remainder.to_s
                errors.add(:iban, " That is not a valid IBAN. The IBAN that is being supplied")
            end

    end

end
Run Code Online (Sandbox Code Playgroud)

Joo*_*aij 6

IBAN工具宝石可用于这一目的,它工作得很好.

要在Rails中使用gem,我建议编写一个验证器类.

首先把它放在你的Gemfile:

gem 'iban-tools'
Run Code Online (Sandbox Code Playgroud)

并运行bundle.

然后创建一个新目录,app/validatorsiban_validator.rb使用这些内容调用一个文件:

require 'iban-tools'

class IbanValidator < ActiveModel::Validator
  def validate(record)
    unless IBANTools::IBAN.valid?(record.iban)
      record.errors.add :iban, record.errors.generate_message(:iban, :invalid)
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

在你的模型类中放这个:

validates_with IbanValidator
Run Code Online (Sandbox Code Playgroud)