Credit Card Validation via the Luhn Formula--Part 3 of Chapter 7 from Usable Shopping Carts (2/4)

To page 1current pageTo page 3To page 4
[previous] [next]

Usable Shopping Carts, Chapter 7: CC Validation and Verification

ASP (VBScript)

As mentioned previously, we will use a Luhn function to determine base level validity of the credit card number. This function doesn't determine if the credit card number actually exists, but rather that it could mathematically exist:

    Function CheckCCNumLuhn(ccnumber)
      Dim i, w, x, y
      y = 0
      ccnumber = Replace(Replace(Replace(CStr(ccnumber), "-", ""), " ", ""), ".", "")

Initially we accept the credit card number and then strip any extraneous information from the passed value.

      w = 2 * (Len(ccnumber) Mod 2)
      For i = Len(ccnumber) - 1 To 1 Step -1
        x = Mid(ccnumber, i, 1)
        if IsNumeric(x) Then
          Select Case (i Mod 2) + w
            Case 0, 3 
              y = y + CInt(x)
            Case 1, 2 
              x = CInt(x) * 2
              if x > 9 Then   
                y = y + (x \ 10) + (x - 10)
              Else
                y = y + x
              End if
          End Select
        End if
      Next     

To verify the number, it needs to be stepped through, from right to left and have a number of mathematic operations processed against it.

      y = 10 - (y Mod 10)
      if y > 9 Then y = 0
      CheckCC = (CStr(y) = Right(ccnumber, 1))
    End function

Finally we determine whether the integer value computed is equivalent to the final digit of the credit card number. If it is, then the function returns true. This indicates the number is a possible credit card number.


To page 1current pageTo page 3To page 4
[previous] [next]

Created: January 2, 2003
Revised: January 2, 2003

URL: http://webreference.com/programming/carts/chap7/3/2.html