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

  • On-demand Event Event Date: January 28, 2015 Check out this webcast and join Jeff Sloyer, IBM Developer Evangelist and Master Inventor, for a tutorial for building cloud-based applications. Using IBM's platform as a service, Bluemix, Jeff will show you how to architect and assemble cloud-based applications built for cloud scale. Leveraging the power of microservices, developers can quickly translate monolithic applications to a cloud-based microarchitecture. This hour-long session introduces the concepts and …

  • Data integrity and ultra-high performance dictate the success and growth of many companies. One of these companies is BridgePay Network Solutions, a recently launched and rapidly growing financial services organization that allows merchants around the world to process millions of daily credit card transactions. Due to the nature of their business, their IT team needed to strike the perfect balance between meeting regulatory-mandated data security measures with the lowest possible levels of latency and …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date