Validate Singapore FIN using Python

import re

def validate_fin(number):
    # Remove all non-alphanumeric characters from the input
    number = re.sub(r'[^a-zA-Z0-9]', '', number)
    
    # Check if the input is a valid FIN number
    if re.match(r'^[mMfFgG][0-9]{7}[a-zA-Z]$', number):
        # Check the checksum of the FIN number
        fin_arr = list(number)
        fin_arr[1] = str(int(fin_arr[1]) * 2)
        fin_arr[2] = str(int(fin_arr[2]) * 7)
        fin_arr[3] = str(int(fin_arr[3]) * 6)
        fin_arr[4] = str(int(fin_arr[4]) * 5)
        fin_arr[5] = str(int(fin_arr[5]) * 4)
        fin_arr[6] = str(int(fin_arr[6]) * 3)
        fin_arr[7] = str(int(fin_arr[7]) * 2)

        weight = sum([int(i) for i in fin_arr])
        offset = 4 if number[0] in ['T', 'G', 'M'] else 0
        check_code = 'JZIHGFEDCBA'[11 - ((weight + offset) % 11)]

        return check_code == number[-1].upper()

    else:
        return False

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.