Validating Mobile IMEI Number

I recently lost my mobile. I searched about how to lock the mobile. I found some pages like IMEI finder that is interesting. They validate the IMEI using the Luhn algorithm. An IMEI number has only 15 or 17 digits. The IMEI numbers N-1 digits are serial numbers, the last digit is a check digit. The IMEI number consists of a manufacturer identity number, distribution number, and other info.
 
Example
 
IMEI number 3 5 7 8 0 5 0 2 3 9 8 4 9 4 ?
Double every other digit 3 10 7 16 0 10 0 4 3 18 8 8 9 8 ?
Sum all digits 3 + (1 + 0) + 7 + (1+6) + 0 + (1+0)+ 0 + 4 + 3 + (1+8) + 8 + 8 + 9 + 8 + ? = 68
 
 
Add all the odd digits and double the even digits and add the digits of the doubling value. Then finally add all the values. Now it has some value (here 68) so the final digit, the check digit, would come from the difference of the preceding total value and the nearest value of the nearest 10. Here the total is 68, the nearest 10 is 70, the difference is 2.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace IMEI_Number_Finder  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.   
  19.         private void button1_Click(object sender, EventArgs e)  
  20.         {  
  21.             string imei = textBox1.Text.Trim();  
  22.             string sub = "";  
  23.             int total = 0;  
  24.   
  25.             int length = imei.Length - 1;  
  26.              
  27.             if (imei.Length == 15 || imei.Length == 17)  
  28.             {  
  29.                 for (int i = 0; i < length; i++)  
  30.                 {  
  31.                     if (i % 2 != 0)  
  32.                     {  
  33.                         sub = ((int)char.GetNumericValue(imei[i]) * 2).ToString();  
  34.                         total += sub.Length == 2 ? (int)char.GetNumericValue(sub[0]) + (int)char.GetNumericValue(sub[1]) : (int)char.GetNumericValue(sub[0]);  
  35.   
  36.                     }  
  37.                     else  
  38.                         total += ((int)char.GetNumericValue(imei[i]));  
  39.                 }  
  40.   
  41.                 if ((int)char.GetNumericValue(imei[length]) == ((((total / 10) + 1) * 10) - total))  
  42.                 {  
  43.                     MessageBox.Show("Valid IMEI Number");  
  44.   
  45.                 }  
  46.                 else  
  47.                 {  
  48.                     MessageBox.Show("In-Valid IMEI Number");  
  49.                     textBox1.Text = "";  
  50.                      sub = "";  
  51.                      total = 0;  
  52.                      length = 0;  
  53.                 }  
  54.             }  
  55.   
  56.             else  
  57.             {  
  58.                 MessageBox.Show("In-Valid IMEI Number");  
  59.                 textBox1.Text = "";  
  60.                 sub = "";  
  61.                 total = 0;  
  62.                 length = 0;  
  63.             }  
  64.   
  65.         }  
  66.     }  
This is the last digit. This is the validation process of the IMEI.