Last active
February 20, 2020 14:26
-
-
Save hawjeh/16c762cfe4daa6a8419e5244286e62e1 to your computer and use it in GitHub Desktop.
Javascript - Singapore NRIC Checker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function check_nric(nric_number) { | |
| var nric_first = ''; | |
| var nric_last = ''; | |
| var nric_code = -1; | |
| var nric_checksum = -1; | |
| var mod_calculate = -1; | |
| var checksum_boolean = false; | |
| var process_abort = false; | |
| var ST_hash = new Array(); | |
| var FG_hash = new Array(); | |
| var offset = 0; | |
| ST_hash["J"] = 0; | |
| ST_hash["Z"] = 1; | |
| ST_hash["I"] = 2; | |
| ST_hash["H"] = 3; | |
| ST_hash["G"] = 4; | |
| ST_hash["F"] = 5; | |
| ST_hash["E"] = 6; | |
| ST_hash["D"] = 7; | |
| ST_hash["C"] = 8; | |
| ST_hash["B"] = 9; | |
| ST_hash["A"] = 10; | |
| FG_hash["X"] = 0; | |
| FG_hash["W"] = 1; | |
| FG_hash["U"] = 2; | |
| FG_hash["T"] = 3; | |
| FG_hash["R"] = 4; | |
| FG_hash["Q"] = 5; | |
| FG_hash["P"] = 6; | |
| FG_hash["N"] = 7; | |
| FG_hash["M"] = 8; | |
| FG_hash["L"] = 9; | |
| FG_hash["K"] = 10; | |
| nric_number = nric_number.toString(); | |
| if (nric_number.length != 9) { | |
| process_abort = true; | |
| checksum_boolean = false; | |
| } | |
| if (!process_abort) { | |
| nric_first = nric_number.substr(0, 1); | |
| nric_last = nric_number.substr(8, 1); | |
| switch (nric_first.toUpperCase()) { | |
| case "S": | |
| nric_checksum = ST_hash[nric_last]; | |
| break; | |
| case "T": | |
| nric_checksum = ST_hash[nric_last]; | |
| offset = 4; | |
| break; | |
| case "F": | |
| nric_checksum = FG_hash[nric_last]; | |
| break; | |
| case "G": | |
| nric_checksum = FG_hash[nric_last]; | |
| offset = 4; | |
| break; | |
| default: | |
| checksum_boolean = false; | |
| process_abort = true; | |
| break; | |
| } | |
| } | |
| if (!process_abort) { | |
| mod_calculate = nric_number.substr(1, 1) * 2 + | |
| nric_number.substr(2, 1) * 7 + | |
| nric_number.substr(3, 1) * 6 + | |
| nric_number.substr(4, 1) * 5 + | |
| nric_number.substr(5, 1) * 4 + | |
| nric_number.substr(6, 1) * 3 + | |
| nric_number.substr(7, 1) * 2; | |
| nric_code = (mod_calculate + offset) % 11; | |
| if (nric_code == nric_checksum) { | |
| checksum_boolean = true; | |
| } | |
| } | |
| return checksum_boolean; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment