Version 0 of Validating an International Bank Account Number

Updated 2010-09-19 17:40:38 by stever

SRIV Sept 19,2010 I had a need to validate IBAN account numbers, so following the example on Wikipedia I wrote a script to do the task. Feel free to test against your bank account numbers and include the country test in the switch with a note saying it works for you.

#!/usr/bin/env tclsh

# Steve Redler IV  SR Technology Sept 19, 2010
# Validating an International Bank Account Number
# as per http://en.wikipedia.org/wiki/International_Bank_Account_Number

set iban "GB82 WEST 1234 5698 7654 32"
set iban0 [string map {" " ""} [string toupper $iban]]
puts "iban0=$iban0"
switch -- [string range $iban0 0 1] {
  HU {if {[string length $iban0] != 28} {
               puts "IBAN must be 28 characters for Hungary"
               exit 0}        
            }
  DE {if {[string length $iban0] != 22} {
               puts "IBAN must be 22 characters for Germany"
               exit 0}        
            }
  IS {if {[string length $iban0] != 26} {
               puts "IBAN must be 26 characters for Iceland"
               exit 0}        
            } 
  GB {if {[string length $iban0] != 22} {
               puts "IBAN must be 22 characters for United Kingdom"
               exit 0}        
            } 
  PL {if {[string length $iban0] != 28} {
               puts "IBAN must be 28 characters for POLAND"
               exit 0}        
            }                                     
}

set iban1 "[string range $iban0 4 end][string range $iban0 0 3]"
puts "iban1=$iban1"

set iban2 ""
foreach char [split $iban1 ""] {
        set ascii [scan $char %c]
        if {$ascii >= 65} {
                append iban2 [expr $ascii -55]
        } else {
          append iban2 $char        
        }
        puts "ascii $ascii  char $char  $iban2"        
}

set iban2 [string trimleft $iban2 0]
puts "iban2=$iban2"

set modulus [expr $iban2 % 97]
puts "modulus=$modulus"

if {$modulus == 1} {
  puts "IBAN number is valid"        
} else {
  puts "IBAN number is invalid"        
}