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 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
}
}
}
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!
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:
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 ));
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With