Skip to content

Instantly share code, notes, and snippets.

@pierstitus
Created April 26, 2016 12:36
Show Gist options
  • Select an option

  • Save pierstitus/59ab321137877345872a6245a7f2ea4a to your computer and use it in GitHub Desktop.

Select an option

Save pierstitus/59ab321137877345872a6245a7f2ea4a to your computer and use it in GitHub Desktop.
import re
import string
def iban_cepa_check(nr):
""" Check if given IBAN/CETA account number is valid. """
nr = nr.replace(' ', '')
if re.fullmatch('[A-Z]{2}\d{2}[A-Z]{4}\d{10}', nr):
check_org = int(nr[2:4])
nr = nr[4:] + nr[:2]
else:
raise Exception("Invalid format, needs to be like 'NL05 RABO 1234 1234 00' (with or without spaces)")
check = 98 - int(''.join(n if n.isdigit() else str(string.ascii_uppercase.index(n) + 10) for n in nr)) * 100 % 97
if check_org != check:
print('invalid, check value should be %d.' % check)
else:
print('valid')
return check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment