Created
April 5, 2017 08:28
-
-
Save mperlet/f912b1e57d058bd1b07d78ed13de1f23 to your computer and use it in GitHub Desktop.
Python IBAN Checker
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
# Based on http://codereview.stackexchange.com/questions/135366/python-iban-validation | |
import string | |
def is_iban(unchecked_iban): | |
LETTERS = {ord(d): str(i) for i, d in enumerate(string.digits + string.ascii_uppercase)} | |
def _number_iban(iban): | |
return (iban[4:] + iban[:4]).translate(LETTERS) | |
def generate_iban_check_digits(iban): | |
number_iban = _number_iban(iban[:2] + '00' + iban[4:]) | |
return '{:0>2}'.format(98 - (int(number_iban) % 97)) | |
def valid_iban(iban): | |
return int(_number_iban(iban)) % 97 == 1 | |
return generate_iban_check_digits(unchecked_iban) == unchecked_iban[2:4] and valid_iban(unchecked_iban) | |
Author
mperlet
commented
Apr 5, 2017
•
it doesn't account for valid ISO2 country names
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment