Skip to content

Instantly share code, notes, and snippets.

@wangpy
Created December 2, 2010 16:27
Show Gist options
  • Save wangpy/725611 to your computer and use it in GitHub Desktop.
Save wangpy/725611 to your computer and use it in GitHub Desktop.
private bool checkID(string inputStr)
{
int[] alphabetValueArray = {
10, 11, 12, 13, 14, 15, 16, 17,
34, 18, 19, 20, 21, 22, 35, 23,
24, 25, 26, 27, 28, 29, 32, 30,
31, 33 };
int[] weightArray = { 1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1 };
if (inputStr.Length != 10) {
return false;
}
int checkValue = 0;
char[] inputCharArray = inputStr.ToUpper().ToCharArray();
if (inputCharArray[0] < 'A' || inputCharArray[0] > 'Z') {
return false;
}
int alphabetValue = alphabetValueArray[inputCharArray[0]-'A'];
checkValue = (alphabetValue / 10) * weightArray[0] + (alphabetValue % 10) * weightArray[1];
for (int i=1; i<10; i++) {
if (inputCharArray[i] < '0' || inputCharArray[i] > '9') {
return false;
}
int charValue = inputCharArray[i] - '0';
checkValue += charValue * weightArray[i+1];
}
if (0 == (checkValue % 10)) {
return true;
} else {
return false;
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Result: " + checkID(textBox1.Text).ToString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment