Validate Singapore UEN using python

def validate_uen(uen):
    """
    This function validates the given Singapore UEN number.
    Returns True if the UEN is valid, else returns False.
    """
    if not isinstance(uen, str):
        return False
    if len(uen) != 9:
        return False
    if not uen.isnumeric():
        return False
    weights = [2, 7, 6, 5, 4, 3, 2]
    checksum = 0
    for i in range(7):
        checksum += int(uen[i]) * weights[i]
    checksum = (11 - (checksum % 11)) % 10
    return checksum == int(uen[8])

To use this function, simply pass a UEN string as an argument to the validate_uen function. The function returns True if the UEN is valid, and False otherwise.

Here’s an example usage:

uen1 = "201512345G"
uen2 = "T12SS3456L"
uen3 = "12345678X"
print(validate_uen(uen1)) # Output: True
print(validate_uen(uen2)) # Output: True
print(validate_uen(uen3)) # Output: False

Note that this code only checks the format and checksum of the UEN number. It does not verify if the UEN is actually registered with the Singapore government.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.