IMEI Validation Technique (VB.NET)

Discussion in 'Programming' started by initiotech, Feb 14, 2012.

  1. #1
    Hi ,

    I would like to share a simple Technique to check for Valid IMEI Number.

    For those who dont know what IMEI is it is International Mobile Equipment Identity Code is a unique 15 Digit number which is usually found printed on the back side of the GSM/CDMA mobile phones battery.

    The Logic behind identifying a correct IMEI Code is very simple.

    First Ill show you the Structure of the Code and then Then the Automated way to check it.

    AAAAAAAA – BBBBBB – C (This is the Structure)

    AAAAAAAA = Type Allocation Code – The Type Allocation Code is an 8-digit number that identifies a particular model of wireless telephone for use on a GSM wireless network.
    BBBBBB – Serial sequence of the model

    And the most Important of All

    C -The last number of the IMEI is a check digit calculated using the Luhn algorithm.
    The check digit is validated in 3 steps:
    1. Starting from the right, double a digit every second digits (e.g., 8 → 16).
    2. Sum the digits (e.g., 16 → 1 + 6).
    3. Check if the sum is divisible by 10.
    One can calculate the IMEI by choosing the check digit that would give a sum divisible by 10. For the example IMEI 352048021523553,

    [TABLE]
    [TR]
    [TD]IMEI[/TD]
    [TD] 3[/TD]
    [TD]5[/TD]
    [TD]2[/TD]
    [TD]0[/TD]
    [TD]4[/TD]
    [TD] 8[/TD]
    [TD]0[/TD]
    [TD]2[/TD]
    [TD]1[/TD]
    [TD]5[/TD]
    [TD]2[/TD]
    [TD]3[/TD]
    [TD]5[/TD]
    [TD]5[/TD]
    [TD] ?[/TD]
    [/TR]
    [TR]
    [TD]Double Every Second Digit [/TD]
    [TD] 3[/TD]
    [TD]10[/TD]
    [TD]2[/TD]
    [TD]0[/TD]
    [TD]4[/TD]
    [TD]16[/TD]
    [TD]0[/TD]
    [TD]4[/TD]
    [TD]1[/TD]
    [TD]10[/TD]
    [TD]2[/TD]
    [TD]6[/TD]
    [TD]5[/TD]
    [TD]10[/TD]
    [TD] ?[/TD]
    [/TR]
    [TR]
    [TD]Sum digits[/TD]
    [TD="colspan: 15"]3+1+0+2+0+4+1+6+0+4+1+1+0+2+6+5+1+0 = 37[/TD]
    [/TR]
    [/TABLE]

    To make the sum divisible by 10, we set ? = 3, so the IMEI is .352048021523553

    Below is the Code in VB.NET you can use it in ASP.NET also to check a Valid IMEI number
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
                If TextBox1.Text.Length = 15 Then
                              Dim IMEI As String = TextBox1.Text 
                              Dim cnt As Integer = 0 
                              Dim nw As String = “”
                       For Each c As Char In
                              IMEIcnt += 1
                                If cnt Mod 2 <> 0 Then
                                      nw += c
                                Else
                                     Dim d As Integer = Integer.Parse(c) * 2nw += d.ToString()
                                End If
                      Next
    Dim tot As Integer = 0 
    For Each ch As Char In nw.Remove(nw.Length – 1, 1)
              tot +=Integer.Parse(ch)
    Next
    Dim chDigit As Integer = 10 – (tot Mod 10)  
             If chDigit = Integer.Parse(IMEI(IMEI.Length – 1)) Then
                      MsgBox(“Valid”) 
              Else
                      MsgBox(“Invalid”) 
             End If
        End If
    End Sub
    Code (markup):
    For it to work in ASP.NET Put the code in a Button Click..

    Hope this helps..

    The original Post is on my Blog (Few Months Back)
    BLOG(DOT)HEFIN(DOT)IN
     
    initiotech, Feb 14, 2012 IP