WPF Validating 13-digit ISBN


In this post we will look at the validation of a 13-digit ISBN book number. Not all 13-digit numbers are acceptable as ISBN numbers. Our program will ask for an ISBN 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 book, it will just tell you if the number you entered can be assigned to a book.

Below is the screenshot of the program. I have entered the ISBM number of Adam Nathan’s book called WPF 4.5 Unleashed by Sams. The 13-digit ISBN for that book is 978-0-672-33697-3.

Below is the XAML code. Notice that the name of the final TextBlock is txtBlkAnswer.

<Window x:Class="CheatSheetISBN.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:CheatSheetISBN"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Title="CheatSheetISBN" Height="150" Width="300">
    <StackPanel Margin="4,4,4,4">
        <TextBlock TextWrapping="Wrap">ISBN - Enter a 13-digit ISBN number to be verified and press the button. Do not include any dashes.</TextBlock>
        <TextBox  Margin="4,4,4,4"  Name="txtISBN" Width="120"></TextBox>
        <Button Width="100" Click="Button_Click">Validate</Button>
        <TextBlock Margin="4,4,4,4" Name="txtBlkAnswer" Foreground="Black"></TextBlock>
    </StackPanel>
</Window>

Below is the C# code behind. We need to go through, one by one, all 13 digits. We throw an error is any one of them are not digits (0 to 9). We do all of this within a try catch block.

using System;
using System.Windows;
using System.Windows.Media;
namespace CheatSheetISBN
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            txtBlkAnswer.Text = "";
            string ISBN = txtISBN.Text;
            if (ISBN.Length == 13)
            {
                int[] c = new int[13];
                try
                {
                    // convert each character to a number, if possible
                    // and put in array
                    for (int i = 0; i < ISBN.Length; i++)
                    {
                        string s = ISBN.Substring(i, 1);
                        bool r = int.TryParse(s, out int result);
                        if (r)
                        {
                            c[i] = result;
                        }
                        else
                        {
                            throw new System.ArgumentException("Character cannot be a non-digit", s.ToString());
                        }
                    }
                    int total = 0;
                    int multiplier = 0; // multiplier
                    for (int i = 0; i <= 11; i++)
                    {
                        // if even position,  multiplier is 1, else 3
                        multiplier = i % 2 == 0 ? 1 : 3;  // ternary
                        total += c[i] * multiplier;
                    }
                    int remainder = total % 10;  // modulo
                                                 //int check = 0;
                    int check = remainder != 0 ? 10 - remainder : 0;
                    if (check == c[12])
                    {
                        txtBlkAnswer.Foreground = new SolidColorBrush(Colors.DarkGreen);
                        txtBlkAnswer.Text = $"{ISBN} is a valid ISBN";
                    }
                    else
                    {
                        txtBlkAnswer.Foreground = new SolidColorBrush(Colors.Red);
                        txtBlkAnswer.Text = $"{ISBN} is not a valid ISBN";
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "CheatSheetISBN");
                }
            }
            else
            {
                MessageBox.Show("Must be 13 digits");
            }
        }
    }
}

Here is another example.