Last active
December 20, 2019 12:45
-
-
Save alexkiro/3af9afff156fbc1ea954e9ec2d6a4f4a to your computer and use it in GitHub Desktop.
Validate CIF (Romanian VAT number) https://ro.wikipedia.org/wiki/Cod_de_Identificare_Fiscal%C4%83
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
def validate_cif(cif): | |
# Remove RO if it was added | |
cif = str(cif).lower().lstrip("ro") | |
if not cif.isnumeric(): | |
# Must be numeric | |
return False | |
if len(cif) >= 10: | |
# Too long | |
return False | |
if len(cif) <= 1: | |
# Too short | |
return False | |
key = "753217532" | |
computed_check_number = sum( | |
int(i) * int(j) | |
for i, j in zip(cif[::-1][1:], key[::-1]) | |
) * 10 % 11 | |
# Modulo by 10, to handle the case where the check number | |
# is 10. | |
if computed_check_number % 10 != int(cif[-1]): | |
# Self check number isn't correct | |
return False | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment