{"id":101,"date":"2023-03-17T08:28:06","date_gmt":"2023-03-17T08:28:06","guid":{"rendered":"https:\/\/smartsource.com.sg\/blog\/?p=101"},"modified":"2023-03-17T08:28:06","modified_gmt":"2023-03-17T08:28:06","slug":"validate-singapore-uen-using-python","status":"publish","type":"post","link":"https:\/\/smartsource.com.sg\/blog\/index.php\/2023\/03\/17\/validate-singapore-uen-using-python\/","title":{"rendered":"Validate Singapore UEN using python"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>def validate_uen(uen):\r\n    \"\"\"\r\n    This function validates the given Singapore UEN number.\r\n    Returns True if the UEN is valid, else returns False.\r\n    \"\"\"\r\n    if not isinstance(uen, str):\r\n        return False\r\n    if len(uen) != 9:\r\n        return False\r\n    if not uen.isnumeric():\r\n        return False\r\n    weights = &#91;2, 7, 6, 5, 4, 3, 2]\r\n    checksum = 0\r\n    for i in range(7):\r\n        checksum += int(uen&#91;i]) * weights&#91;i]\r\n    checksum = (11 - (checksum % 11)) % 10\r\n    return checksum == int(uen&#91;8])\r\n<\/code><\/pre>\n\n\n\n<p>To use this function, simply pass a UEN string as an argument to the <code>validate_uen<\/code> function. The function returns <code>True<\/code> if the UEN is valid, and <code>False<\/code> otherwise.<\/p>\n\n\n\n<p>Here&#8217;s an example usage:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>uen1 = \"201512345G\"\r\nuen2 = \"T12SS3456L\"\r\nuen3 = \"12345678X\"\r\nprint(validate_uen(uen1)) # Output: True\r\nprint(validate_uen(uen2)) # Output: True\r\nprint(validate_uen(uen3)) # Output: False\r<\/code><\/pre>\n\n\n\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s an&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[64,83],"class_list":["post-101","post","type-post","status-publish","format-standard","hentry","category-tutorials","tag-python","tag-uen"],"_links":{"self":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=101"}],"version-history":[{"count":1,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":102,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts\/101\/revisions\/102"}],"wp:attachment":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}