Check Credit Card Number

' ' Usage: Returns Boolean indicating whether or not credit card ' number given passes the Luhn Formula (as described in ' ISO/IEC 7812-1:1993). Only numbers and numerical strings ' accepted, remove any spaces and dashes in strings. ' Example: ' booAcceptCard = CheckCC("4000000000000002") ' Function CheckCC(CCNo) Dim I, w, x, y ' y = 0 CCNo = CStr(CCNo) ' 'Process digits from right to left, drop last digit if total 'length is even ' w = 2 * (len(CCNo) Mod 2) ' for I = len(CCNo) - 1 to 1 step -1 x = mid(CCNo, I, 1) ' If IsNumeric(x) then Select Case (I Mod 2) + w Case 0, 3 ' 'Even Digit - Odd where total length is odd '(eg. Visa vs. Amx) ' y = y + CInt(x) Case 1, 2 ' 'Odd Digit - Even where total length is odd ' (eg. Visa vs. Amx) ' x = CInt(x) * 2 If x > 9 then ' 'Break the digits (eg. 19 becomes 1 + 9) ' y = y + (x \ 10) + (x - 10) else y = y + x End If End Select End If next ' 'Return the 10's complement of the total y = 10 - (y Mod 10) ' If y > 9 then y = 0 CheckCC = (CStr(y) = Right$(CCNo, 1)) End Function


Comments

  • cards

    Posted by Legacy on 12/08/2003 12:00am

    Originally posted by: gabreal

    how to check remaining amount in a creditcard
    

    Reply
  • fnLuhnCheck

    Posted by Legacy on 10/22/2003 12:00am

    Originally posted by: Aijaz Ahmed

    I am chaking the Luhn of credit card The code is given below
    plz suggest me where i am making mistake and what?

    Private Function fnLuhnCheck(strCCNumber As String, strCCType As String) As Boolean
    Dim strRev As String
    Dim strCh As String
    Dim intNumber As Integer
    Dim strNumberFinal As Integer
    Dim intsum As Integer
    Dim validLuhn As Boolean
    strRev = StrReverse(strCCNumber)
    For intTemp = 1 To Len(strRev)
    strCh = Mid(strRev, intTemp, 1)
    intNumber = CInt(strCh)
    If intTemp Mod 2 = 0 Then
    intNumber = intNumber * 2
    If intNumber > 9 Then
    intNumber = intNumber - 9
    End If
    End If
    strNumberFinal = strNumberFinal & intNumber
    Next intTemp
    For intTemp = 1 To Len(strNumberFinal)
    intsum = intsum + Mid(strNumberFinal, intTemp, 1)
    Next intTemp
    If intsum Mod 10 = 0 Then
    validLuhn = True
    Else
    validLuhn = False
    End If
    fnLuhnCheck = validLuhn

    End Function

    Reply
  • fnLuhnCheck

    Posted by Legacy on 10/22/2003 12:00am

    Originally posted by: Aijaz Ahmed

    I am chaking the Luhn of credit card The code is given below
    plz suggest me where i am making mistake and what?

    Private Function fnLuhnCheck(strCCNumber As String, strCCType As String) As Boolean
    Dim strRev As String
    Dim strCh As String
    Dim intNumber As Integer
    Dim strNumberFinal As Integer
    Dim intsum As Integer
    Dim validLuhn As Boolean
    strRev = StrReverse(strCCNumber)
    For intTemp = 1 To Len(strRev)
    strCh = Mid(strRev, intTemp, 1)
    intNumber = CInt(strCh)
    If intTemp Mod 2 = 0 Then
    intNumber = intNumber * 2
    If intNumber > 9 Then
    intNumber = intNumber - 9
    End If
    End If
    strNumberFinal = strNumberFinal & intNumber
    Next intTemp
    For intTemp = 1 To Len(strNumberFinal)
    intsum = intsum + Mid(strNumberFinal, intTemp, 1)
    Next intTemp
    If intsum Mod 10 = 0 Then
    validLuhn = True
    Else
    validLuhn = False
    End If
    fnLuhnCheck = validLuhn

    End Function

    Reply
  • Formation criteria of a sequence of numbers!! Help me!!

    Posted by Legacy on 08/30/2003 12:00am

    Originally posted by: Tiago

    Does anybody know how may I check the formation criteria of a sequence of numbers??!?! For example, I have 5 different sequences of numbers and the 5 sequences have the same formation criteria. Like the credit card numbers, for example... thanks!
    

    Reply
  • What if the Credit Card is expired?

    Posted by Legacy on 02/24/2003 12:00am

    Originally posted by: Bryan

    So how can you check if the Card has already expired?

    Reply
  • another way...

    Posted by Legacy on 03/27/2001 12:00am

    Originally posted by: Ben

    Using the simpler way 
    
    (http://beaver.dburry.com/tutorial/ccvalidate.html) as a base I wrote this quick function in VB which does the same thing as the one in this artical:

    Function mod10 ( num As String ) As Boolean
    On Error GoTo mod10_err
    Dim sum As Integer
    Dim i As Integer

    num = trim(num)
    If Len(num) Mod 2 > 0 Then num = "0" + num
    For i = Len(num) To 1 Step -2
    sum = sum + Cint(Mid$(num, i, 1)) + (Cint(Mid$(num, i - 1, 1)) * 2)
    If (Cint(Mid$(num, i - 1, 1)) >= 5) Then sum = sum - 9
    Next
    mod10 = (( sum Mod 10 ) = 0 )

    Exit Function
    mod10_err:
    ' we probably got here because the 'number' contained non-numeric characters
    mod10 = FALSE
    End Function

    Reply
  • simpler way to do this

    Posted by Legacy on 12/27/2000 12:00am

    Originally posted by: Dave

    Sorry it's not in the same language but there is a simpler way to do this illustrated at http://beaver.dburry.com/tutorial/ccvalidate.html

    Dave

    Reply
  • Check Credit Card Number

    Posted by Legacy on 11/13/2000 12:00am

    Originally posted by: Michael

    Very good depiction of the luhn formula

    good work

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Instead of only managing projects organizations do need to manage value! "Doing the right things" and "doing things right" are the essential ingredients for successful software and systems delivery. Unfortunately, with distributed delivery spanning multiple disciplines, geographies and time zones, many organizations struggle with teams working in silos, broken lines of communication, lack of collaboration, inadequate traceability, and poor project visibility. This often results in organizations "doing the …

  • Live Event Date: April 22, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Database professionals — whether developers or DBAs — can often save valuable time by learning to get the most from their new or existing productivity tools. Whether you're responsible for managing database projects, performing database health checks and reporting, analyzing code, or measuring software engineering metrics, it's likely you're not taking advantage of some of the lesser-known features of Toad from Dell. Attend this live …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds