-
-
Save wilane/37f32e422f62aa3a226b6cbc718f9ca6 to your computer and use it in GitHub Desktop.
Python IBAN Checker
This file contains 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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment