Validation: Speed Up Credit Card Processing
When a credit card won't process, sometimes it's for the sinister reasons we all suspect:
The cardholder is over limit.
The expiration date has passed.
The account has been closed.
Some form of fraud is being attempted.
Far more often, though, the reason is simple: People make mistakes.
But sinister or simple, mistakes your customers make when entering their credit card information can cost you time and money.
Luckily, credit card numbers are configured in such a way that by performing some simple math, you can quickly and easily separate out card numbers fraught with errors and typos from potentially good card numbers.
A Trick for Your Bag
Every card number can be added up using a particular procedure and the result used as an at-a-glance indicator of whether the card number might be valid or bogus.
The procedure is known as a Mod10 or Luhn10 check. You can use a calculator to come up with your own results manually.
And for the programmers in the audience, I provide sample code written in VBScript. With just a little work, you can adapt this sample code for use in your ASP pages. Or use this example as a guide when rewriting this function in the programming language of your choice.
Before we begin, a word of caution: Just because a card number passes this test does not mean you can rest assured the card is valid. Only obtaining an authorization can do that. Rather, failing this test means there's no way the card could possibly be valid.
This test can serve as a first line of defense against errors and the simplest of fraud, but it is no match for today's breed of creative fraudsters.
Luhn10
At its simplest, Luhn10 is the process of adding a credit card's digits together, dividing by 10, and seeing if there's a remainder. If the digits divide cleanly, the card might be valid; but if there's a remainder, the card is bad or you're bad at math.
Here are the steps:
- Add from the right side of the number.
- Begin with the second digit from the right side and multiply every other digit by 2. It's important to begin from the right because card numbers vary in length.
- For the numbers you multiply by 2, you break the result into digits and add the individual digits into your total. Example: If the number is 8, then 2 x 8 = 16. But instead of adding 16 to your total, add 1 plus 6.
So let's look at an example, using a fake number: 43130716741528.
To make the first rule easier to handle, let's start by reversing the number so we can work from left to right, as we're accustomed, and breaking it into digits: 8 2 5 1 4 7 6 1 7 0 3 1 3 4.
Now let's multiply every other digit by two: 8 4 5 2 4 14 6 2 7 0 3 2 3 8.
Notice that there's no special handling for zeros that are to be doubled: as any former dot-com employee in this economy will tell you, two times nothing is still nothing. Also notice that one of the digits that got doubled was a seven, and that means that as we add everything up, we need to split that 14 into its digits:
8 + 4 + 5 + 2 + 4 + (1+4) + 6 + 2 + 7 + 0 + 3 + 2 + 3 + 8 = 59.
Next, we'll divide by 10 and look for a remainder. Computers have special functions that do this, but on your calculator you'll look to see if there's a nonzero digit to the right of your decimal point: 59 divided by 10 = 5.9.
Because there's a 9 to the right of the decimal place, we know right away that this test number is invalid.
ASP Sample Code
Of course, performing this check for every order you receive would be a real drag. What follows is sample code written in VBScript that automates the process. To see this code in action, cut and paste it into a text file, name it something like "card-validator.vbs," and double click the file.
Adjust the value of the variable "strCardNum" on the third line to test the script with other card numbers. To use this code in your Web site's ASP pages, you'll need to copy the function into your page and call that function at an appropriate time, passing in the card number you want to validate.
Descriptive comments in the code are preceded by an apostrophe.
' BEGIN
' First we'll create a fake test card
dim strCardNum
strCardNum = "43130716741528"
' Now we'll use a function (defined below)
' to validate the test card number.
boolResult = funcValidateCard (strCardNum)
msgBox("Potentially Valid Card Number?" & vbcrlf & vbcrlf & boolResult)
' Finally, here's the function where all
' the work gets done.
function funcValidateCard (strCard)
dim intCardLength, intSum, intDigit
' first reverse the string
strCard = StrReverse(strCard)
' Step through every digit in the number.
intCardLength = Len(strCard)
i = 1
While i <= intCardLength
' Determine if this is an even space.
if i Mod 2 = 0 then
' Double every second space.
intWeight = 2
else
' Simply add in odd spaces.
intWeight = 1
end if
' Grab the next digit and multiply
' by the weight
intDigit = Mid(strCard,i,1) * intWeight
' This bit of magic is equivalent to
' separating the result into digits
' before adding it to our
' running total.
intSum = intSum + _
(intDigit Mod 10) + _
Int(intDigit / 10)
' Move to the next digit.
i = i + 1
Wend
if intSum Mod 10 = 0 then
funcValidateCard = true
else
funcValidateCard = false
end if
end function
' END
Remember, there is no substitute for obtaining an authorization when it comes to validating card numbers.
But when it's impractical or you simply don't want to incur the overhead and expense of obtaining a live authorization, this calculation can be a quick way to separate the credit card wheat from the chaff.




