Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Check validity of Singapore NRIC number
""" This module has utility methods related to Singapore NRIC number"""
def check_nric_sg(nric):
""" check if a nric is valid or not"""
list_m = [2, 7, 6, 5, 4, 3, 2]
list_st = list('JZIHGFEDCBA')
list_fg = list('XWUTRQPNMLK')
if len(nric) != 9:
return False
nric = nric.upper()
if nric[0] not in ['S', 'T', 'F', 'G']:
return False
list_nric = list(nric[1:8])
result = sum([int(a)*b for a, b in zip(list_nric, list_m)])
if nric[0] == 'T' or nric[0] == 'G':
result += 4
result_mod = result % 11
if nric[0] == "S" or nric[0] == "T":
checksum = list_st[result_mod]
elif nric[0] == "F" or nric[0] == "G":
checksum = list_fg[result_mod]
return nric[8] == checksum
def fix_nric_sg(nric):
"""Fix the first char of the NRIC as many patients don't input that"""
if not check_nric_sg(nric):
for prefix in ['S', 'T', 'F', 'G']:
nric_temp = prefix + nric
if check_nric_sg(nric_temp):
return nric_temp
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment