Skip to content

Instantly share code, notes, and snippets.

@alimardani1994
Last active April 11, 2023 07:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alimardani1994/7e6eb5ff0f04495cd4f1c969fd928c51 to your computer and use it in GitHub Desktop.
Save alimardani1994/7e6eb5ff0f04495cd4f1c969fd928c51 to your computer and use it in GitHub Desktop.
چک کردن صحت کد ملی و شناسه اقتصادی
public class NationalCodeUtil {
private final static String notNationalCode[] = new String[]{
"0000000000",
"1111111111",
"2222222222",
"3333333333",
"4444444444",
"5555555555",
"6666666666",
"7777777777",
"8888888888",
"9999999999",
};
public static boolean checkNationalCode(String nationalCode) {
if (nationalCode == null)
return false;
if (nationalCode.isEmpty())
return false;
if (nationalCode.length() < 8 || nationalCode.length() > 10)
return false;
if (!nationalCode.matches("[0-9]+"))
return false;
if (nationalCode.length() < 10 && nationalCode.length() >= 8)
nationalCode = String.format("%010d", Integer.parseInt(nationalCode));
for (String s : notNationalCode) {
if (s.equalsIgnoreCase(nationalCode))
return false;
}
String nationalCodeWithoutControlDigit = nationalCode.substring(0, nationalCode.length() - 1);
String controlDigit = nationalCode.substring(nationalCode.length() - 1, nationalCode.length());
int sum = 0;
int i = 10;
for (char c : nationalCodeWithoutControlDigit.toCharArray()) {
int temp = Integer.parseInt("" + c) * i;
i--;
sum += temp;
}
int modBy11 = sum % 11;
if (modBy11 < 2) {
if (modBy11 == Integer.parseInt(controlDigit))
return true;
} else if (11 - modBy11 == Integer.parseInt(controlDigit))
return true;
return false;
}
public static boolean checkNationalEconomicalCode(String nationalCode) {
if (nationalCode == null)
return false;
if (nationalCode.isEmpty())
return false;
if (nationalCode.length() > 11)
return false;
if (nationalCode.length() < 11)
return false;
if (!nationalCode.matches("[0-9]+"))
return false;
String nationalCodeWithoutControlDigit = nationalCode.substring(0, nationalCode.length() - 1);
String controlDigit = nationalCode.substring(nationalCode.length() - 1, nationalCode.length());
String deci = nationalCode.substring(nationalCode.length() - 2, nationalCode.length() - 1);
int decimal = Integer.parseInt(deci) + 2;
int multiplier[] = {29, 27, 23, 19, 17, 29, 27, 23, 19, 17};
int sum = 0;
int i = 0;
for (char c : nationalCodeWithoutControlDigit.toCharArray()) {
int temp = (Integer.parseInt("" + c) + decimal) * multiplier[i];
i++;
sum += temp;
}
int modBy11 = sum % 11;
if (modBy11 == 10) {
modBy11 = 0;
}
if (modBy11 == Integer.parseInt(controlDigit))
return true;
return false;
}
}
@pouyan021
Copy link

سلام. این الگوریتم کد اقتصادی رو چک میکنه یا شناسه ملی رو؟
در ضمن الگوریتم کد ملیتون درست نیست
link

@Alireza-Noorali
Copy link

سلام. متد اعتبارسنجی کد ملی درست تر رو میتونید از این آدرس داشته باشید:
https://stackoverflow.com/a/44603571/6444297

@iman2420
Copy link

سلام. این الگوریتم کد اقتصادی رو چک میکنه یا شناسه ملی رو؟

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment