Skip to content

Instantly share code, notes, and snippets.

@kamerk22
Last active Aug 25, 2017
Embed
What would you like to do?
Validation for Singapore NRIC and FIN number in PHP
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