Friday, December 4, 2009

Singapore NRIC check

There are various nric check information pages, but most of them are giving you the algo. Here is one implemented in PHP.


<?php
function check_nric($nric) {
  if ( preg_match('/^[ST][0-9]{7}[JZIHGFEDCBA]$/', $nric) ) 
  { // NRIC
    $check = "JZIHGFEDCBA";
  } else if ( preg_match('/^[FG][0-9]{7}[XWUTRQPNMLK]$/', $nric) ) 
  { // FIN
    $check = "XWUTRQPNMLK";
  } else 
  {
    return false;
  }

  $total = $nric[1]*2
    + $nric[2]*7
    + $nric[3]*6
    + $nric[4]*5
    + $nric[5]*4
    + $nric[6]*3
    + $nric[7]*2;

  if ( $nric[0] == "T" OR $nric[0] == "G" ) 
  {
    // shift 4 places for after year 2000
    $total = $total + 4; 
  }

  if ( $nric[8] == $check[$total % 11] ) {
    return TRUE;
  } else {
    return FALSE;
  }
}
?>

No comments:

Post a Comment