Skip to content

Instantly share code, notes, and snippets.

@xq5he
Created March 7, 2016 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xq5he/ff0c9c0b48e95b53607b to your computer and use it in GitHub Desktop.
Save xq5he/ff0c9c0b48e95b53607b to your computer and use it in GitHub Desktop.
Validate Mainland China ID Number
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