Skip to content

Instantly share code, notes, and snippets.

@sabman
Forked from koos/gist:921728
Created August 15, 2012 15:45
Show Gist options
  • Save sabman/3361112 to your computer and use it in GitHub Desktop.
Save sabman/3361112 to your computer and use it in GitHub Desktop.
validates_each :iban do | record, attr, value |
record.errors.add attr, 'IBAN is mandatory' and next if value.blank?
# IBAN code should start with country code (2letters)
record.errors.add attr, 'Country code is missing from the IBAN code' and next unless value.to_s =~ /^[A-Z]{2}/i
iban = value.gsub(/[A-Z]/) { |p| (p.respond_to?(:ord) ? p.ord : p[0]) - 55 }
record.errors.add attr, 'Invalid IBAN format' unless (iban[6..iban.length-1].to_s+iban[0..5].to_s).to_i % 97 == 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment