Skip to content

Instantly share code, notes, and snippets.

@cmariod
Created June 7, 2016 16:17
Show Gist options
  • Save cmariod/61ecb5a025d48e838b7920f900d76de2 to your computer and use it in GitHub Desktop.
Save cmariod/61ecb5a025d48e838b7920f900d76de2 to your computer and use it in GitHub Desktop.
<?php
class Nric_validation{
function is_NRIC($nric)
{
if(strlen($nric)==9) // NRIC is alway 9 Digits
{
$nric = strtoupper($nric);
$prefix = substr($nric,0,1);
$postfix = substr($nric,8,1);
$ic_number = substr($nric,1,7);
$num = 0;
$weight = array(2,7,6,5,4,3,2);
$lookup['S'] = array('J','Z','I','H','G','F','E','D','C','B','A');
$lookup['T'] = array('G','F','E','D','C','B','A','J','Z','I','H');
$lookup['F'] = array('X','W','U','T','R','Q','P','N','M','L','K');
$lookup['G'] = array('R','Q','P','N','M','L','K','X','W','U','T');
for($i=0;isset($weight[$i]);$i++)
{
$num+=substr($ic_number,$i,1)*$weight[$i];
}
$num %= 11;
if (isset($lookup[$prefix]))
{
if($lookup[$prefix][$num] == $postfix)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment