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

  • Are you truly leading your team or simply managing them? Organizations need leaders and your team needs someone to follow. With some ongoing development, you could become that leader. Learn the top leadership qualities that inspire others to want to follow you and the direction of your company.

  • Who can you trust? Learn from the IBM X-Force team in this new quarterly report how the Internet of Things and IP reputation tracking are transforming the security landscape.

Most Popular Programming Stories

More for Developers

RSS Feeds

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