Is it possible to validate IMEI Number?
Asked Answered
H

7

11

For a mobile shop application, I need to validate an IMEI number. I know how to validate based on input length, but is their any other mechanism for validating the input number? Is there any built-in function that can achieve this?

Logic from any language is accepted, and appreciated.

Hirsute answered 10/8, 2014 at 14:37 Comment(3)
If you just want to check the number has been entered correctly, en.wikipedia.org/wiki/IMEI.Kipper
possible duplicate of Check for valid IMEIBowlin
That answer does not meet my requirement, the question is not properly answered only a hint is given. i got a little better answer for my question. that i acceptedHirsute
B
35

A search suggests that there isn't a built-in function that will validate an IMEI number, but there is a validation method using the Luhn algorithm.

General process:

  1. Input IMEI: 490154203237518
  2. Take off the last digit, and remember it: 49015420323751 & 8. This last digit 8 is the validation digit.
  3. Double each second digit in the IMEI: 4 18 0 2 5 8 2 0 3 4 3 14 5 2 (excluding the validation digit)
  4. Separate this number into single digits: 4 1 8 0 2 5 8 2 0 3 4 3 1 4 5 2 (notice that 18 and 14 have been split).
  5. Add up all the numbers: 4+1+8+0+2+5+8+2+0+3+4+3+1+4+5+2 = 52
  6. Take your resulting number, remember it, and round it up to the nearest multiple of ten: 60.
  7. Subtract your original number from the rounded-up number: 60 - 52 = 8.
  8. Compare the result to your original validation digit. If the two numbers match, your IMEI is valid.

The IMEI given in step 1 above is valid, because the number found in step #7 is 8, which matches the validation digit.

Bowlin answered 10/8, 2014 at 14:53 Comment(2)
I step 6, it should be nearest next multiple of ten.Saadi
In step 8, it should be if they do not match, your IMEI is invalid.Cancroid
S
5

According to the previous answer from Karl Nicoll i'm created this method in Java.

public static int validateImei(String imei) {

    //si la longitud del imei es distinta de 15 es invalido
    if (imei.length() != 15)
        return CheckImei.SHORT_IMEI;

    //si el imei contiene letras es invalido
    if (!PhoneNumber.allNumbers(imei))
        return CheckImei.MALFORMED_IMEI;

    //obtener el ultimo digito como numero
    int last = imei.charAt(14) - 48;

    //duplicar cada segundo digito
    //sumar cada uno de los digitos resultantes del nuevo imei
    int curr;
    int sum = 0;
    for (int i = 0; i < 14; i++) {
        curr = imei.charAt(i) - 48;
        if (i % 2 != 0){
            // sum += duplicateAndSum(curr);
            // initial code from Osvel Alvarez Jacomino contains 'duplicateAndSum' method.
            // replacing it with the implementation down here:
            curr = 2 * curr;
            if(curr > 9) {
                curr = (curr / 10) + (curr - 10);
            }
            sum += curr;
        }
        else {
            sum += curr;
        }

    }

    //redondear al multiplo de 10 superior mas cercano
    int round = sum % 10 == 0 ? sum : ((sum / 10 + 1) * 10);

    return (round - sum == last) ? CheckImei.VALID_IMEI_NO_NETWORK : CheckImei.INVALID_IMEI;

}
Stomatic answered 26/8, 2018 at 20:26 Comment(0)
Q
2

IMEI can start with 0 digit. This is why the function input is string. Thanks for the method @KarlNicol

Golang

func IsValid(imei string) bool {
    digits := strings.Split(imei, "")

    numOfDigits := len(digits)

    if numOfDigits != 15 {
        return false
    }

    checkingDigit, err := strconv.ParseInt(digits[numOfDigits-1], 10, 8)
    if err != nil {
        return false
    }

    checkSum := int64(0)
    for i := 0; i < numOfDigits-1; i++ { // we dont need the last one
        convertedDigit := ""

        if (i+1)%2 == 0 {
            d, err := strconv.ParseInt(digits[i], 10, 8)
            if err != nil {
                return false
            }
            convertedDigit = strconv.FormatInt(2*d, 10)
        } else {
            convertedDigit = digits[i]
        }

        convertedDigits := strings.Split(convertedDigit, "")

        for _, c := range convertedDigits {
            d, err := strconv.ParseInt(c, 10, 8)
            if err != nil {
                return false
            }
            checkSum = checkSum + d
        }

    }

    if (checkSum+checkingDigit)%10 != 0 {
        return false
    }

    return true
}
Quarterback answered 9/4, 2020 at 8:58 Comment(0)
M
0

I think this logic is not right because this working only for the specific IMEI no - 490154203237518 not for other IMEI no ...I implement the code also...

var number = 490154203237518;
var array1 = new Array();
var array2 = new Array();
var specialno = 0 ;
var sum = 0 ;
var finalsum = 0;
var cast = number.toString(10).split('');
var finalnumber = '';
if(cast.length == 15){
    for(var i=0,n = cast.length; i<n; i++){

        if(i !== 14){
          if(i == 0 || i%2 == 0 ){
            array1[i] = cast[i];
          }else{
            array1[i] = cast[i]*2;
          }
        }else{
           specialno = cast[14];
        }

     }

     for(var j=0,m = array1.length; j<m; j++){
        finalnumber = finalnumber.concat(array1[j]);
     }

     while(finalnumber){
        finalsum += finalnumber % 10;
        finalnumber = Math.floor(finalnumber / 10);
     }

    contno = (finalsum/10);
    finalcontno = Math.round(contno)+1;

    check_specialno = (finalcontno*10) - finalsum; 

    if(check_specialno == specialno){
        alert('Imei')
    }else{
        alert('Not IMEI');
    }
}else{
    alert('NOT imei - length not matching');
}   


 //alert(sum);
Malorie answered 26/7, 2017 at 13:4 Comment(0)
A
0

According to the previous answer from Karl Nicoll i'm created this function in Python.

from typing import List


def is_valid_imei(imei: str) -> bool:
    def digits_of(s: str) -> List[int]:
        return [int(d) for d in s]

    if len(imei) != 15 or not imei.isdecimal():
        return False

    digits = digits_of(imei)
    last = digits.pop()
    for i in range(1, len(digits), 2):
        digits[i] *= 2
    digits = digits_of(''.join(map(str, digits)))
    return (sum(digits) + last) % 10 == 0
Aceldama answered 17/5, 2022 at 13:1 Comment(0)
D
0

This one is working for me

 try:
        immie_procesed = list(
            map(int, str(request.data.decode("utf-8").split(',')[0])))
        last_number = immie_procesed[len(immie_procesed) - 1]
        val = sum([sum(map(int, str(int(v)*2))) if i % 2 else int(v)
                  for i, v in enumerate(immie_procesed[: -1])])
        round_value = math.ceil(val / 10) * 10
        validation_value = round_value - val
        if (validation_value == last_number):
            IMEI = request.data.decode("utf-8").split(',')[0]
        else:
            return "INVALID IMEI"
    except:
        return 'Something goes wrong in validation process'
Daybreak answered 15/11, 2022 at 14:1 Comment(0)
H
-1

I don't believe there are any built-in ways to authenticate an IMEI number. You would need to verify against a third party database (googling suggests there are a number of such services, but presumably they also get their information from more centralised sources).

Helaine answered 10/8, 2014 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.