How to validate iranian Melli Code (National Code or Code Melli) in android
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
}
Good Luck!
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)));
}
}
}
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 ));
}