Featured Posts

Web Presense Solution I've officially started up my independent web development side-business. I am hoping to bring quite a few small businesses into the present with a simple, cost-effective package that provides them with...

Readmore

MS SQL date / time conversion I often find myself turning to MSDN to get the possible conversion options on MS SQL's date data type, so I finally became un-lazy and created a little script to use in the future to quickly list all possibilities...

Readmore

jQuery Delay Happy Note: I was contacted by the jQuery team and my code is being added to the core functionality; that really made my day!  You will want to remove the current code from your site if you plan on upgrading...

Readmore

ASP Search Stemmer Class The original stemmer class was developed by Martin Porter to bring words back to their word stems. For example "abilities" would stem to "able", "smelling" to "smell", "I'm awesome" to "damn straight",...

Readmore

  • Prev
  • Next

How to check for legit credit card numbers

Posted on : 30-07-2009 | By : chelfers | In : ASP, Web

0

I think the title says it all; I've seen many implementations for checking credit card numbers and in my opinion there is really only one that you need and actually works.

Sadly, still in use today by many code examples that I've run across is the good old

if ( first_digit = 4 ) then "visa" elseif ( first_digit = 5 ) then "mastercard" elseif ( first_digit = 3 ) then "amex" elseif ( first_digit = 6 ) then "discover" else "invalid card" end if

/facepalm

This isn't entirely bad ( yes it is, I'm just trying to be nice ), most likely those first digits will always be associated with the cards mentioned above, the problem is the numbers that follow.

"But I found one that checks and second and third numbers!"

Awesome, one tiny step closer to almost not really getting anywhere!

Obviously I'm leading up to the one and only Luhn algorithm which is basically a simple checksum formula used to validate a few different sequences of numbers, for example credit card account numbers.

Straight from Wikipedia here is what is going on with the algorithm

The formula verifies a number against its included check digit, which is usually appended to a partial account number to generate the full account number. This account number must pass the following test:

1. Counting from the check digit, which is the rightmost, and moving left, double the value of every second digit.
2. Sum the digits of the products together with the undoubled digits from the original number.
3. If the total ends in 0 (put another way, if the total modulo 10 is congruent to 0), then the number is valid according to the Luhn formula; else it is not valid.

As an illustration, if the account number is 49927398716, it will be validated as follows:

1. Double every second digit, from the rightmost: (1×2) = 2, (8×2) = 16, (3×2) = 6, (2×2) = 4, (9×2) = 18
2. Sum all the individual digits (digits in parentheses are the products from Step 1): 6 + (2) + 7 + (1+6) + 9 + (6) + 7 + (4) + 9 + (1+8) + 4 = 70
3. Take the sum modulo 10: 70 mod 10 = 0; the account number is valid.

Below is an ASP version I've ported, enjoy.

ACCOUNT = "4500000000000001" alt = false sum = 0     for i = len( ACCOUNT ) to 1 step - 1             digit = mid( ACCOUNT, i, 1 )                     if ( alt = true ) then                 digit = digit * 2                     if ( digit > 9 ) then digit = digit - 9 end if             end if         sum = ( sum + digit ) mod 10                                                 if ( alt = true ) then alt = false else alt = true     next if ( sum = 0 ) then   'legit else   'did not pass the test end if

Write a comment