In this post we will look at the validation of a 9-digit Social Insurance Number (SIN) from Canada. Our program will ask for a Canadian SIN number and then run some tests on it to see if it is valid. It won’t tell you if that particular number is assigned to a person, it will just tell you if the number you entered can be assigned to a person.
Social Insurance Numbers can be validated through a simple check digit process called the Luhn algorithm. Here is an example of a fictitious, but valid SIN: 046 454 286. Below is the explanation from Wikipedia on SIN.
- 046 454 286 <--- A fictitious, but valid SIN
- 121 212 121 <--- Multiply each digit in the top number by the digit below it.
- So the result of the multiplication is:
- 0 8 6 8 5 8 2 16 6
- Then, add all of the digits together (note that 16 is 1+6):
- 0 + 8 + 6 + 8 + 5 + 8 + 2 + 1+6 + 6 = 50
- If the SIN is valid, this number will be evenly divisible by 10.
Below is the screenshot of the program. I have entered
Below is the XAML code. There is nothing particularly interesting about the XAML code, except that we need to Name the TextBox that the users enters data into and we need to name the TextBlock that we show the results with.
<Window x:Class="CheatSheetValidateSIN.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:CheatSheetValidateSIN" mc:Ignorable="d" WindowStartupLocation="CenterScreen" Title="CheatSheetValidateSIN" Height="150" Width="350"> <StackPanel Margin="4,4,4,4"> <TextBlock TextWrapping="Wrap">Canadian SIN - Enter a 9-digit SIN number to be verified and press the button. Do not include any dashes or spaces. </TextBlock> <TextBox Margin="4,4,4,4" Name="txtValidateSIN" Width="120"></TextBox> <Button Name="btnValidateSIN" Width="100" Click="BtnValidateSIN_Click">Validate</Button> <TextBlock Margin="4,4,4,4" Name="txtBlkSINAnswer" Foreground="Black"></TextBlock> </StackPanel> </Window>
Below is the code.
using System; using System.Windows; using System.Windows.Media; namespace CheatSheetValidateSIN { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void BtnValidateSIN_Click(object sender, RoutedEventArgs e) { txtBlkSINAnswer.Text = ""; string SIN = txtValidateSIN.Text; if (SIN.Length == 9) { string LastDigitOfSIN = SIN.Substring(8); int doubleddigit = 0; int total = 0; try { // check that all characters are digits (0 to 9) and throw exeception if not. for (int i = 0; i <= 8; i += 1) { string s = SIN.Substring(i, 1); bool b = int.TryParse(s, out int result); if (!b) { throw new System.ArgumentException("Character cannot be a non-digit", s.ToString()); } } for (int i = 0; i <= 8; i += 2) //1st,3rd,5th,7th,9th { string s2 = SIN.Substring(i, 1); bool b = int.TryParse(s2, out int result); total += result; } for (int i = 1; i <= 7; i += 2) //2nd,4th,6th,8th { string s = SIN.Substring(i, 1); bool b = int.TryParse(s, out int result); doubleddigit = result * 2; if (doubleddigit > 9) doubleddigit -= 9; // if we get two digits, we add the digits together which is the same thing // as subtracting 9 total += doubleddigit; } // valid SIN totals are evenly divisible by 10. Use modulo. if ((total % 10) == 0) { txtBlkSINAnswer.Foreground = new SolidColorBrush(Colors.DarkGreen); txtBlkSINAnswer.Text = SIN + " is valid"; } else { txtBlkSINAnswer.Foreground = new SolidColorBrush(Colors.Red); txtBlkSINAnswer.Text = SIN + " is not valid"; } } catch (Exception ex) { MessageBox.Show(ex.Message, "CheatSheetValidateSIN"); } } else { MessageBox.Show("SIN must be 9 digits in lenth. No spaces, no dashes."); } } } }