با استفاده از کد ذیل قابلیت بررسی شناسه ملی اشخاص حقوقی در کشور ایران توسط کاربران فراهم خواهد شد
string strcode = textBoxX1.Text; // code shenase melli
int D = int.Parse(strcode[9].ToString()) + 2;
int[] Zarib = new int[] { 29, 27, 23, 19, 17 };
int zc = 0;
int sum = 0;
if (strcode != "00000000000" || strcode != "11111111111" || strcode != "22222222222" ||
strcode != "33333333333" || strcode != "44444444444" || strcode != "55555555555" ||
strcode != "66666666666" || strcode != "77777777777" || strcode != "88888888888" ||
strcode != "99999999999")
{
for (int i = 0; i < strcode.Length - 1; i++)
{
if (zc > 4) zc = 0;
sum += (D + int.Parse(strcode[i].ToString())) * Zarib[zc];
listBox1.Items.Add((D + int.Parse(strcode[i].ToString())) * Zarib[zc]);
zC++;
}
if (sum % D == int.Parse(strcode[10].ToString()))
{
MessageBox.Show("صحیح است");
}
else
{
MessageBox.Show("صحیح نیست");
}
}
else
{
MessageBox.Show("صحیح نیست");
}
به شکل یک کلاس همانند ذیل اقدام نمائید :
public static Boolean checkLegalNationalCode(String nationalCode)
{
if (nationalCode.Length < 11 || int.Parse(nationalCode) == 0)
return false;
if (int.Parse(nationalCode.Substring(3, 9)) == 0)
return false;
int c = int.Parse(nationalCode.Substring(10, 11));
int d = int.Parse(nationalCode.Substring(9, 10)) + 2;
int[] z = new int[] { 29, 27, 23, 19, 17 };
int s = 0;
for (byte i = 0; i < 10; i++)
s += (d + int.Parse(nationalCode.Substring(i, i + 1))) * z[i % 5];
s = s % 11;
if (s == 10)
s = 0;
return (c == s);
}

ورود به سایت