This article describes how to validate a UDID number (similar to the Social Security Number in the US) using C# in a client-side C# application. We can validate the Aadhaar number using a Verhoeff algorithm before sending it to the server.

First design a Windows Form.

Aadhaar Number Validation Using C#

Back-end Code

  1. private void textBox1_TextChanged(object sender, EventArgs e)
  2. {
  3. if (textBox1.Text.Length == 4)
  4. textBox2.Focus();
  5. }
  6. private void textBox2_TextChanged(object sender, EventArgs e)
  7. {
  8. if (textBox2.Text.Length == 4)
  9. textBox3.Focus();
  10. if (textBox2.Text.Length == 0)
  11. textBox1.Focus();
  12. }
  13. private void textBox3_TextChanged(object sender, EventArgs e)
  14. {
  15. if (textBox3.Text.Length == 0)
  16. textBox2.Focus();
  17. }
  18. private void button1_Click(object sender, EventArgs e)
  19. {
  20. string sAdhaar = textBox1.Text + textBox2.Text + textBox3.Text;
  21. if (sAdhaar.Length == 12)
  22. {
  23. bool isValidnumber = aadharcard.validateVerhoeff(sAdhaar);
  24. if (isValidnumber)
  25. {
  26. label2.ForeColor = Color.Green;
  27. label2.Text = "Aadhaar Validation Success";
  28. }
  29. else
  30. {
  31. label2.ForeColor = Color.Red;
  32. label2.Text = "Invalid Aadhaar Number";
  33. }
  34. }
  35. else
  36. {
  37. label2.Text = "Enter Adhaar Number";
  38. label2.ForeColor = Color.Red;
  39. }
  40. }

Create a new class file with the name aadharcard.cs.

  1. public class aadharcard
  2. {
  3. static int[,] d = new int[,]
  4. {
  5. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
  6. {1, 2, 3, 4, 0, 6, 7, 8, 9, 5},
  7. {2, 3, 4, 0, 1, 7, 8, 9, 5, 6},
  8. {3, 4, 0, 1, 2, 8, 9, 5, 6, 7},
  9. {4, 0, 1, 2, 3, 9, 5, 6, 7, 8},
  10. {5, 9, 8, 7, 6, 0, 4, 3, 2, 1},
  11. {6, 5, 9, 8, 7, 1, 0, 4, 3, 2},
  12. {7, 6, 5, 9, 8, 2, 1, 0, 4, 3},
  13. {8, 7, 6, 5, 9, 3, 2, 1, 0, 4},
  14. {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
  15. };
  16. static int[,] p = new int[,]
  17. {
  18. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
  19. {1, 5, 7, 6, 2, 8, 3, 0, 9, 4},
  20. {5, 8, 0, 3, 7, 9, 6, 1, 4, 2},
  21. {8, 9, 1, 6, 0, 4, 3, 5, 2, 7},
  22. {9, 4, 5, 3, 1, 2, 6, 8, 7, 0},
  23. {4, 2, 8, 6, 5, 7, 3, 9, 0, 1},
  24. {2, 7, 9, 3, 8, 0, 6, 4, 1, 5},
  25. {7, 0, 4, 6, 9, 1, 3, 2, 5, 8}
  26. };
  27. static int[] inv = { 0, 4, 3, 2, 1, 5, 6, 7, 8, 9 };
  28. public static bool validateVerhoeff(string num)
  29. {
  30. int c = 0; int[] myArray = StringToReversedIntArray(num);
  31. for (int i = 0; i < myArray.Length; i++)
  32. {
  33. c = d[c, p[(i % 8), myArray[i]]];
  34. } return c == 0;
  35. }
  36. private static int[] StringToReversedIntArray(string num)
  37. {
  38. int[] myArray = new int[num.Length];
  39. for (int i = 0; i < num.Length; i++)
  40. {
  41. myArray[i] = int.Parse(num.Substring(i, 1));
  42. }
  43. Array.Reverse(myArray); return myArray;
  44. }
  45. }

Result

Invalid Aadhaar Number -

Aadhaar Number Validation Using C#

Valid Aadhaar Number -

Aadhaar Number Validation Using C#