如何在Android中验证伊朗梅利代码(国家代码或代码梅利) [英] How to validate iranian Melli Code (National Code or Code Melli) in android

查看:84
本文介绍了如何在Android中验证伊朗梅利代码(国家代码或代码梅利)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来验证伊朗人的Melli代码(国家代码或Melli代码).

I need a method to validate the Melli Code (National Code or Code Melli) of iranian people.

我知道它有一个公式.

推荐答案

此方法验证了伊朗人民的Melli码.

This method validates the Iranian people's Melli code.

public boolean validateMelliCode(String melliCode) {

    String[] identicalDigits = {"0000000000", "1111111111", "2222222222", "3333333333", "4444444444", "5555555555", "6666666666", "7777777777", "8888888888", "9999999999"};

    if (melliCode.trim().isEmpty()) {
        Toast.makeText(getApplicationContext(), "Melli Code is empty", Toast.LENGTH_LONG).show();
        return false; // Melli Code is empty
    } else if (melliCode.length() != 10) {
        Toast.makeText(getApplicationContext(), "Melli Code must be exactly 10 digits", Toast.LENGTH_LONG).show();
        return false; // Melli Code is less or more than 10 digits
    } else if (Arrays.asList(identicalDigits).contains(melliCode)) {
        Toast.makeText(getApplicationContext(), "MelliCode is not valid (Fake MelliCode)", Toast.LENGTH_LONG).show();
        return false; // Fake Melli Code
    } else {
        int sum = 0;

        for (int i = 0; i < 9; i++) {
            sum += Character.getNumericValue(melliCode.charAt(i)) * (10 - i);
        }

        int lastDigit;
        int divideRemaining = sum % 11;

        if (divideRemaining < 2) {
            lastDigit = divideRemaining;
        } else {
            lastDigit = 11 - (divideRemaining);
        }

        if (Character.getNumericValue(melliCode.charAt(9)) == lastDigit) {
            Toast.makeText(getApplicationContext(), "MelliCode is valid", Toast.LENGTH_LONG).show();
            return true;
        } else {
            Toast.makeText(getApplicationContext(), "MelliCode is not valid", Toast.LENGTH_LONG).show();
            return false; // Invalid MelliCode
        }
    }
}

更新

国家/地区代码,因此我们必须接受由重复数字组成的国家/地区代码.所以我们不需要这个数组:

UPDATE

This authentic news agency said there is a man who has "1111111111" national code, so we have to accept the national codes composed of repetitive digits. So we don't need this Array:

String[] identicalDigits = {"0000000000", "1111111111", "2222222222", "3333333333", "4444444444", "5555555555", "6666666666", "7777777777", "8888888888", "9999999999"};

我们也不需要这部分条件:

and also we don't need this part of condition:

else if (Arrays.asList(identicalDigits).contains(melliCode)) {
        Toast.makeText(getApplicationContext(), "MelliCode is not valid (Fake MelliCode)", Toast.LENGTH_LONG).show();
        return false; // Fake Melli Code
    } 

祝你好运!

这篇关于如何在Android中验证伊朗梅利代码(国家代码或代码梅利)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆