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.

Revisions

  1. cmariod created this gist Jun 7, 2016.
    45 changes: 45 additions & 0 deletions nric_validation.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    <?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;
    }

    }

    }
    }