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

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Corporate e-Learning technology has a long and diverse pedigree. As far back as the 1980s, companies were adopting computer-based training to supplement traditional classroom activities. More recently, rich web-based applications have added streaming audio and video, real-time collaboration and other new tools to the e-Learning mix. At the same time, the growing availability of informal learning tools--a category that includes everything from web searches to social media posts--are having a major impact on …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds