IMEI Validation - Luhn Check

Validate mobile IMEI numbers with this simple Luhn validation

<?php
function validate_imei($imei)
{
	if (!preg_match('/^[0-9]{15}$/', $imei)) return false;
	$sum = 0;
	for ($i = 0; $i < 14; $i++)
	{
		$num = $imei[$i];
		if (($i % 2) != 0)
		{
			$num = $imei[$i] * 2;
			if ($num > 9)
			{
				$num = (string) $num;
				$num = $num[0] + $num[1];
			}
		}
		$sum += $num;
	}
	if ((($sum + $imei[14]) % 10) != 0) return false;
	return true;
}
?>

Usage

Just call the function...


$imei = '123456789123456';
var_dump(validate_imei($imei));

// output:
false


Comments

guest
Posted on 20.06.2014 09:50

Thanks very useful function, saved me time :)

guest
Posted on 21.02.2013 17:06

Can you give an working example for noobs ?
Thanks.

Add your comment