Location: PHPKode > scripts > SIN/NAS Validator > sin.php
<?php
//Simple method to validate Social Insurance Numbers (Canada only)
//The SIN can be 000000000 or 000-000-000
//Written by Ian Lord (hide@address.com)
//Based on document found here: http://www.pwgsc.gc.ca/compensation/ppim/ppim-5-1-e.html

class SIN {
	
	private $SIN;
	
	public function __construct($value) {
		$this->SIN = str_replace('-','',$value);	
	}
	
	public function __toString () {
		return substr($this->SIN,0,3).'-'.substr($this->SIN,3,3).'-'.substr($this->SIN,6,3);
	}
	
	public function validate () {

		IF ((is_numeric($this->SIN) && $this->SIN >= 100000000 && $this->SIN <= 999999999)){
			$a_SIN = str_split($this->SIN);

			if (($a_SIN[1] *= 2)>= 10){
				$a_SIN[1] -= 9;
			}
			if (($a_SIN[3] *= 2)>= 10){
				$a_SIN[3] -= 9;
			}
			if (($a_SIN[5] *= 2)>= 10){
				$a_SIN[5] -= 9;
			}
			if (($a_SIN[7] *= 2)>= 10){
				$a_SIN[7] -= 9;
			}

			if (array_sum($a_SIN) % 10 != 0) {
				return false;
			}
			return true;

		}
		else {
			return false;
		}
	}
}

$invalidSIN = new SIN('123456789');
$validSIN = new SIN('440968592');
$invalidSIN2 = new SIN('123-456-789');
$validSIN2 = new SIN('440-968-592');

if (!$invalidSIN->validate()){
	echo $invalidSIN;
	echo " is not a valid SIN\n";
}

if ($validSIN->validate()){
	echo $validSIN;
	echo "  is a valid SIN\n";
}

if (!$invalidSIN2->validate()){
	echo $invalidSIN2;
	echo " is not a valid SIN\n";
}

if ($validSIN2->validate()){
	echo $validSIN;
	echo " is a valid SIN\n";
}

echo $validSIN;
?>

Return current item: SIN/NAS Validator