Location: PHPKode > scripts > Singapore NRIC > class.nric.php
<?php

/*~ class.csvlist.php
.---------------------------------------------------------------------------.
|  Software: NRIC verification class                                        |
|   Version: 1.0                                                            |
|   Contact: Gary Tay <garytay81(at)gmail(dot)com>                          |
|   Require: PHP                                                            |
|       Use: Class to verify if it's a valid singaporean identity card no.  |
|                                                                           |
|      Date: 28 June 2007                                                   |
| ------------------------------------------------------------------------- |
| This program is distributed in the hope that it will be useful - WITHOUT  |
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     |
| FITNESS FOR A PARTICULAR PURPOSE.                                         |
'---------------------------------------------------------------------------'

The Singapore NRIC number is made up of 7 digits and a letter behind. 
This letter is calculated from the first 7 digits using the modulus eleven method, 
similar to the method used for calculating the check digit of the ISBN Code.

The steps involved to obtain the check digit is

   1. Multiply each digit in the NRIC number by its weight.
   2. Add together the above products.
   3. Divide the resulting sum by 11.
   4. Subtract the remainder from 11 to give the check digit.
   5. Check the check digit against the table to obtain the alphabet.


*/

 
class nric
{

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);
 
		$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;$weight[$i];$i++)
		{
		$num+=substr($ic_number,$i,1)*$weight[$i];
		}
 
		$num %= 11;
 
		if($lookup[$prefix][$num]==$postfix)
		{
			return true;
		}else
		{
			return false;
		}
 
	}
 
}

}

/* Sample

$nric = new nric();

if($nric->is_NRIC("S2624053J")) //Random NRIC
{echo "Correct NRIC";}
else
{echo "Your NRIC is wrong";}

*/
?>
Return current item: Singapore NRIC