Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile

Mobile IMEI Validation

5.00/5 (3 votes)
21 Dec 2011CPOL 11.9K  
There is my version, no use of int arrays. While the while instruction is a good approach in many cases, in this specific one, it is not necessary, since the bigger number will be 18 (9 * 2):private Boolean ValidateIMEI(string IMEI){ if (IMEI.Length != 15) return false; var sum...

There is my version, no use of int arrays. While the while instruction is a good approach in many cases, in this specific one, it is not necessary, since the bigger number will be 18 (9 * 2):


C#
private Boolean ValidateIMEI(string IMEI)
{
  if (IMEI.Length != 15)
    return false;

  var sum = 0;
  var pos = 0;
  foreach (var imeiChar in IMEI)
  {
    var imeiInt = int.Parse(imeiChar.ToString());
    if (pos % 2 != 0) imeiInt *= 2;
    if (imeiInt > 9) imeiInt = (imeiInt % 10) + (imeiInt / 10);
    sum += imeiInt;
    ++pos;
  }
  return sum % 10 == 0;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)