Created
September 7, 2012 08:02
-
-
Save kasperhartwich/3664247 to your computer and use it in GitHub Desktop.
PHP function to validate IMEI numbers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function validateImei($imei, $use_checksum = true) { | |
if (is_string($imei)) { | |
if (ereg('^[0-9]{15}$', $imei)) { | |
if (!$use_checksum) return true; | |
for ($i = 0, $sum = 0; $i < 14; $i++) { | |
$tmp = $imei[$i] * (($i%2) + 1 ); | |
$sum += ($tmp%10) + intval($tmp/10); | |
} | |
return (((10 - ($sum%10)) %10) == $imei[14]); | |
} | |
} | |
return false; | |
} |
Be careful, IMEI can be 15 or 17 number lenght.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TNX