Created
March 7, 2016 07:04
-
-
Save xq5he/ff0c9c0b48e95b53607b to your computer and use it in GitHub Desktop.
Validate Mainland China ID Number
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
def is_valid_id_number(id): | |
""" | |
Validate Mainland China ID Number | |
:param id: Identity Number | |
:return: True if id is a valid identity number, else False | |
""" | |
if len(id) != 18: | |
return False | |
check_digits = ('1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2') | |
factor = (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2) | |
remainder = sum([int(v)*factor[i] for i, v in enumerate(id[:-1])]) % 11 | |
return check_digits[remainder] == id[-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment