Created
April 15, 2011 13:51
-
-
Save koos/921728 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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