Is it possible to validate IMEI Number?

后端 未结 5 916
时光取名叫无心
时光取名叫无心 2021-02-09 09:40

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?

5条回答
  •  灰色年华
    2021-02-09 10:21

    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;
    
    }
    

提交回复
热议问题