Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate Iranian National Code (Melli Code or Code Melli) in android

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

I Know it has a formula.

like image 215
Alireza Noorali Avatar asked Jun 17 '17 10:06

Alireza Noorali


4 Answers

This method validates the Iranian people's National 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(), "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
        }
    }
}

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 National Code
    } 

Good Luck!

like image 89
Alireza Noorali Avatar answered Nov 14 '22 15:11

Alireza Noorali


Good job Alireza, Here is my code which is very similar to yours.

‍‍‍

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

    }
  }
like image 34
Arash Avatar answered Nov 14 '22 16:11

Arash


You can validate your national code like this:

Updated

public static boolean isValidNationalCode(String nationalCode)
{
      if (!nationalCode.matches("^\\d{10}$"))
          return false;

      int sum = 0;

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

      int lastDigit = Integer.parseInt(String.valueOf(nationalCode.charAt(9)));
      int divideRemaining = sum % 11;

      return ((divideRemaining < 2 && lastDigit == divideRemaining) ||   (divideRemaining >= 2 && (11 - divideRemaining) == lastDigit ));
}
like image 4
Mohammad Moradi Avatar answered Nov 14 '22 16:11

Mohammad Moradi


I have different versions of it here and the following is its Kotlin one,

fun isValidIranianNationalCode(input: String) = input.takeIf { it.length == 10 }
    ?.mapNotNull(Char::digitToIntOrNull)?.takeIf { it.size == 10 }?.let {
        val check = it[9]
        val sum = it.slice(0..8).mapIndexed { i, x -> x * (10 - i) }.sum() % 11
        if (sum < 2) check == sum else check + sum == 11
    } ?: false
like image 4
Ebrahim Byagowi Avatar answered Nov 14 '22 16:11

Ebrahim Byagowi