Luhn Algorithm Validation in VBScript (ASP)

The Luhn Algorithm is a simple checksum formula that is used to verify a variety of different numbers including most credit cards. Outside of verifying funds it provides the most accurate means of verifying credit card numbers by preventing typographical errors.

In a simple checksum, each digit in a number is added up to find a sum of all digits. That value is then used to validate the number later. Typically the checksum is used to find a modulus value that is then appended to the original number as a check digit. The Luhn Algorithm works a little differently.

With the Luhn Algorithm a number is processed one digit at a time from right to left. Every second digit is first doubled before being added to the final sum. If that doubling results in a two digit number, the digits in the result are added together to result in a single digit. If the sum of all digits is evenly divisible by 10, the number is considered to be valid. Let’s look at an example.

Consider the number: 5780 2415

Beginning from right to left, add the digits one at a time. The first digit, 5, is added to the running total. The second digit, 1, is first doubled. So 2 is now added to the 5 from the last digit for a running total of 7. The next digit, 4, makes that total 11. Every other digit is first doubled, so the 2 becomes a 4 for a total of 15.

A zero is then added and the total remains at 15. The next digit, 8, is doubled for a value of 16. Since this results in a two-digit number, the digits are added to make 7 (1 + 6). This 7 is then added to the total making it 22. The remaining digits, 7 and 5, add 7 and 1 respectively making the final sum 30.

Since the checksum value of 30 is evenly divisible by 10, the original number passes the Luhn Algorithm test.

Now let’s see how this looks in code!

Function LuhnCheck(strNumber)
	' Use a quick regular expression to remove any non-digit characters
	Set RegEx = New RegExp
	RegEx.Global = True
	RegEx.Pattern = "[\D\s]+"
	strNumber = RegEx.Replace(strNumber, "")
	Set RegEx = Nothing
 
	' Grab the length of the string of numbers
	intLength = Len(strNumber)
	' Grab the parity (Whether the first digit in an even or odd position
	'   within the string)
	intParity = intLength Mod 2
 
	' Loop across each digit in the number string
	For i = 1 To intLength
		' Mid is used to grab a single digit
		intDigit = CInt(Mid(strNumber, i, 1))
		' Parity is used to determine every other digit from the right
		If i Mod 2 <> intParity Then
			' Double every other digit
			intDigit = intDigit * 2
			' if that value is two digits...(greater than 9)
			If intDigit > 9 Then
				'...add the digits together (same as subtracting 9)
				'  e.g.  consider 12.  1+2=3 or 12-9=3
				intDigit = intDigit - 9
			End If
		End If
		intSum = intSum + intDigit
	Next
 
	' True if number is evenly divisble by 10, false otherwise
	LuhnCheck = (intSum Mod 10 = 0)
End Function

You may need to re-read my explanation of the Luhn Algorithm above, but the comments in the code should give you a full explanation.

Tags

Like the read? Share it!

2 Comments

  • Your code does not match the text. The text says process each character from right to left, the code is processing then from left to right. Which is right?

  • The code processes from left to right, but it’s done using logic. I’ve first determined the parity of each digit (it’s even or odd position from the right).

    Since addition is cumulative, you can add from the left to the right or from the right to the left and come up with the same sum as long as you know the correct parity for processing each digit.

    I’ve used this approach to save on processing requirements as opposed to reversing the string or reading it directly from right to left (which requires reversing the string internally). The key is that finding a digit’s parity achieves the same effect.

Leave a Reply

Contact

Wanna say hello?
Drop us a line!

You'll find us here

1 Microsoft Way,
Redmond,
WA 98052, United States