Last active
August 25, 2017 11:17
-
-
Save kamerk22/ed5e0778b3723311d8dd074c792834ef to your computer and use it in GitHub Desktop.
Validation for Singapore NRIC and FIN number in PHP
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
public static function validIdentification($number) | |
{ | |
if (strlen($number) !== 9) { | |
return false; | |
} | |
$newNumber = strtoupper($number); | |
$icArray = []; | |
for ($i = 0; $i < 9; $i++) { | |
$icArray[$i] = $newNumber{$i}; | |
} | |
$icArray[1] = intval($icArray[1], 10) * 2; | |
$icArray[2] = intval($icArray[2], 10) * 7; | |
$icArray[3] = intval($icArray[3], 10) * 6; | |
$icArray[4] = intval($icArray[4], 10) * 5; | |
$icArray[5] = intval($icArray[5], 10) * 4; | |
$icArray[6] = intval($icArray[6], 10) * 3; | |
$icArray[7] = intval($icArray[7], 10) * 2; | |
$weight = 0; | |
for ($i = 1; $i < 8; $i++) { | |
$weight += $icArray[$i]; | |
} | |
$offset = ($icArray[0] === "T" || $icArray[0] == "G") ? 4 : 0; | |
$temp = ($offset + $weight) % 11; | |
$st = ["J", "Z", "I", "H", "G", "F", "E", "D", "C", "B", "A"]; | |
$fg = ["X", "W", "U", "T", "R", "Q", "P", "N", "M", "L", "K"]; | |
$theAlpha = ""; | |
if ($icArray[0] == "S" || $icArray[0] == "T") { | |
$theAlpha = $st[$temp]; | |
} else if ($icArray[0] == "F" || $icArray[0] == "G") { | |
$theAlpha = $fg[$temp]; | |
} | |
return ($icArray[8] === $theAlpha); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment