{"id":99,"date":"2023-03-17T08:24:26","date_gmt":"2023-03-17T08:24:26","guid":{"rendered":"https:\/\/smartsource.com.sg\/blog\/?p=99"},"modified":"2023-03-17T08:24:26","modified_gmt":"2023-03-17T08:24:26","slug":"python-code-for-validation-of-nric","status":"publish","type":"post","link":"https:\/\/smartsource.com.sg\/blog\/index.php\/2023\/03\/17\/python-code-for-validation-of-nric\/","title":{"rendered":"Python code for validation of NRIC"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>import re\r\n\r\ndef validate_nric(number):\r\n    # Remove all non-alphanumeric characters from the input\r\n    number = re.sub(r'&#91;^a-zA-Z0-9]', '', number)\r\n    \r\n    # Check if the input is a valid NRIC number\r\n    if re.match(r'^&#91;stfgSTFG]&#91;0-9]{7}&#91;a-zA-Z]$', number):\r\n        # Check the checksum of the NRIC number\r\n        nric_arr = list(number)\r\n        nric_arr&#91;1] = str(int(nric_arr&#91;1]) * 2)\r\n        nric_arr&#91;2] = str(int(nric_arr&#91;2]) * 7)\r\n        nric_arr&#91;3] = str(int(nric_arr&#91;3]) * 6)\r\n        nric_arr&#91;4] = str(int(nric_arr&#91;4]) * 5)\r\n        nric_arr&#91;5] = str(int(nric_arr&#91;5]) * 4)\r\n        nric_arr&#91;6] = str(int(nric_arr&#91;6]) * 3)\r\n        nric_arr&#91;7] = str(int(nric_arr&#91;7]) * 2)\r\n\r\n        weight = sum(&#91;int(i) for i in nric_arr])\r\n        offset = 4 if number&#91;0] in &#91;'T', 'G'] else 0\r\n        check_code = 'JZIHGFEDCBA'&#91;11 - ((weight + offset) % 11)]\r\n\r\n        return check_code == number&#91;-1].upper()\r\n\r\n    else:\r\n        return False\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[82,64],"class_list":["post-99","post","type-post","status-publish","format-standard","hentry","category-tutorials","tag-nric","tag-python"],"_links":{"self":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts\/99","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=99"}],"version-history":[{"count":1,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":100,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts\/99\/revisions\/100"}],"wp:attachment":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}