如何在android中验证伊朗国家代码(Melli代码或代码Melli)

2022-09-03 09:50:23

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

我知道它有一个公式。


答案 1

此方法验证伊朗人民的国家代码。

public boolean validateMelliCode(String melliCode) {

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

    if (melliCode.trim().isEmpty()) {
        Toast.makeText(getApplicationContext(), "National Code is empty", Toast.LENGTH_LONG).show();
        return false; // National Code is empty
    } else if (melliCode.length() != 10) {
        Toast.makeText(getApplicationContext(), "National Code must be exactly 10 digits", Toast.LENGTH_LONG).show();
        return false; // National 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 National 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
        }
    }
}

更新

这家正宗的新闻机构说,有一个人有国家代码,所以我们不得不接受由重复数字组成的国家代码。所以我们不需要这个数组:"1111111111"

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

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

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

祝你好运!


答案 2

干得好 Alireza,这是我的代码,与你的非常相似。

‍‍‍

private boolean isValidNationalCode(String nationalCode) {
    if (nationalCode.length() != 10) {
      return false;
    } else {
      //Check for equal numbers
      String[] allDigitEqual = {"0000000000", "1111111111", "2222222222", "3333333333",
          "4444444444", "5555555555", "6666666666", "7777777777", "8888888888", "9999999999"};
      if (Arrays.asList(allDigitEqual).contains(nationalCode)) {
        return false;
      } else {
        int sum = 0;
        int lenght = 10;
        for (int i = 0; i < lenght - 1; i++) {
          sum += Integer.parseInt(String.valueOf(nationalCode.charAt(i))) * (lenght - i);
        }

        int r = Integer.parseInt(String.valueOf(nationalCode.charAt(9)));

        int c = sum % 11;

        return (((c < 2) && (r == c)) || ((c >= 2) && ((11 - c) == r)));
      }

    }
  }

推荐