It’s not often that I post some code here on iGeek, which is a pitty because coding is a real passion of mine specially PHP, but sometimes you just write something you need to share with your fellow enthusiasts.

Here is a function I wrote in PHP that validates South African ID Numbers based on Luhn Check:

function ValidateSAIDNumber($idnr) {
	$d = -1;
	$a = 0;
	for($i=0;$i<6;$i++)
		$a += substr($idnr,$i*2,1);
	for($i=0;$i<6;$i++)
		$b = $b * 10 + substr($idnr,(2*$i)+1,1);
	$b *= 2;
	$c = 0;

	while($b > 0) {
		$c += $b % 10;
		$b = $b / 10;
	}
	$c += $a;
	$d = 10 - ($c % 10);
	if($d == 10)
		$d = 0;
	if($d == substr($idnr,strlen($idnr)-1,1))
		return true;
	else
		return false;
}

To use simply call ValidateSAIDNumber(IDNR) where IDNR is the South African ID Number and it will either return true if its a valid number or false if its not.