I need a method to validate the Melli Code (National Code or Code Melli) of iranian people.
I Know it has a formula.
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
}
}
}
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
}
Good Luck!