LUHN Validation for ASP.NET (Web Forms and MVC)

February 1, 2012

In this post, I thought I'd look at a complete server-side and client-side solution for validating credit card numbers using the LUHN algorithm for both ASP.NET Web Forms and MVC. The core code uses some nice LINQ that I have previously blogged about, but thought it was worth providing a complete example to demonstrate how to leverage the validation infrastructure in the various ASP.NET technologies.

LUHN Algorithm

The LUHN algorithm is a popular way to validate credit card numbers. I’ve used it many times while developing e-commerce applications to check that a user has entered their credit card number correctly. By using the LUHN algorithm to verify a card number, you can let a customer know their card number is invalid before taking payment through a gateway. After all, it’s a better user experience if they don’t have to wait for the server to try and authorize their card through a payment gateway with incorrect details that could have been detected using a simple LUHN check!

Yes that’s right, the LUHN algorithm is very simple, despite a lot of example code making it look hard to implement!

The LUHN formula produces a checksum that can be used to determine if a credit card number is valid. It’s not bullet proof, but it is good at catching genuine user input errors.

The LUHN checksum is calculated by first reversing the credit card number. Secondly, in the reversed number every other digit is doubled. This means the first digit remains the same, the second digit is doubled, the third digit stays the same, the fourth is doubled, etc. This leaves you with a set of numbers some of which will be two digit numbers, some single digit numbers. Thirdly, take all the digits in the set of numbers from the second step and add them up (you will treat each digit in the two digit numbers as two single digits when adding everything up). Finally, take the sum module 10 and this gives the LUHN checksum.

The final thing to note with the LUHN algorithm is that valid credit card numbers have a checksum of zero.

LUHN algorithm in C# and LINQ

This LUHN utility class provides a robust and complete means of checking the validity of a credit card number using LINQ. It uses several maths tricks and properties of the LUHN formula to reduce the LINQ statement down to two steps.

The utility also optionally allows spaces as some payment gateways are tolerant of spaces and some users like to put spaces when entering their card details.

The method correctly rejects non-numeric input and rogue characters. The two guards in the prevent non-numeric digits from accidentally validating and this also means that I can make the LINQ statement quite optimised because by the time the LINQ statement executes, the string is guaranteed to consist only of digits.

LUHN algorithm in JavaScript

If we want to roll the LINQ and C# implementation into some validation logic for ASP.NET Web Forms or MVC that supports client-side validation we also need a JavaScript version of the LUHN formula: