A check digit is a decimal (or alphanumeric) digit added to a number for the purpose of detecting the sorts of errors that humans typically make on data entry. Check digits are used for many purposes, such as credit card numbers and International Standard Book Numbers (ISBN). There are a number of schemes and algorithms used for calculating check digits. Some useful references are:
This article presents a C# class that can be used to calculate and verify check digits using the Verhoeff scheme.
On a recent project for an appointment booking system I needed to allocate a "booking identification number" to each appointment. When a client arrived for their appointment, the client would enter their booking identification number at a kiosk using a touch screen. It was important to protect against common data entry errors. The inclusion of a check digit in the number seemed sensible.
After researching various check digits schemes, the most appropriate was the Verhoeff scheme. The benefits of this scheme include:
- It catches more errors than most other schemes (schemes that add more than one check digit can catch more errors, at the expense of adding more digits)
- It only adds decimal digits (0-9), so does not require an alpha "keyboard" on the touch screen display
- It can be used on a number with any number of digits
I found an implementation of the Verhoeff scheme in Java code here. This was easily converted to C# and the code provided with this article was based on the Java code.
Using the code
The source code for this arcticle includes two projects.
- The first project is a class library that provides the implementation of the Verhoeff scheme. This project provides an assembly that can be reused with any application that needs to calculate or validate check digits.
- The second project is a WinForms application that can be used to enter a number and calculate its check digit, or check whether a number includes a valid check digit. This applications demonstrates the use of the public interface provided by the class library project.
To use the code in your own project you can either use the class library project as a separate assembly for your application and reference it from your project. Alternatively you can copy just the VerhoeffCheckDigit.cs code file into your own project.
Depending on how you want to calculate, store and verify the check digit of a number, use the appropriate methods provided by the VerhoeffCheckDigit class. This will depend on the data type you want to use for your numbers ande whether you want to include the check digit as part of the number to which it applies (appended at the end) or keep it separate.
In the class library project, the
VerhoeffCheckDigit class implements the algorithm. Static methods are provided to calculate and verify check digits.
AppendCheckDigit method calculates the Verhoeff check digit for a given number provided as input, then returns the input with the check digit appended at the end. Four overloads of this method allow for different data types as input and return value.
string - This allows a check digit to be calculated for an arbitrary long number
int - The check digit is added as the last element in the array
CalculateCheckDigit method calculates and returns the Verhoeff check digit for a given number provided as input. Four overloads of this method provide for different data types as input. The return value is an int for each overload.
Check method verifies the check digit for a number. Eight overloads of this method provide for different data types as input, and whether the check digit is included as part of the input number or as a separate number. The return value is a bool for each overload: (
true if the check digit is valid,
false otherwise). Use one of the first four overloads if the check digit is the last digit in the input. Use one of the last four overloads if the check digit is separate to the input.
The Verhoeff scheme for check digits is a great choice for many common scenarios where you want to detect the sorts of errors that humans typically make on data entry. If you need to implement this in your application I hope this article is of some help.