Luhn Check - Credit Card Checker Validation

Share your advanced PureBasic knowledge/code with the community.
Straker
Enthusiast
Enthusiast
Posts: 701
Joined: Wed Apr 13, 2005 10:45 pm
Location: Idaho, USA

Luhn Check - Credit Card Checker Validation

Post by Straker »

Yes, I did search but did not locate Joakim's entry in a different thread until I had written my own, so....

Here is a Luhn algorithm, also known as "Modulus 10" or "Mod 10", which I converted from JavaScript:

Code: Select all

Procedure.l LuhnCheck(pNumber.s)
  ; Luhn algorithm number checker - converted from JavaScript by Straker.
  ; 10-digit number checksum used for validating credit cards, etc.
  ; for more info see: http://en.wikipedia.org/wiki/Luhn_algorithm
  ;
  ; original JavaScript code by shaman @ www.planzero.org:
  ; http://planzero.org/code/bits/viewcode.php?src=luhn_check.js
  ; 
  ; 11-03-2008
  ; 
  ; Returns 1 if number is valid, otherwise 0
  ;
  Protected lRetVal.l, lNumLen.l, lTotal.l, lLoop.l
  Protected lDigit.l
  
  lRetVal.l = 0
  
  ; Strip any non-digits (useful For credit card numbers With spaces And hyphens)
  
  pNumber.s = ReplaceString ( pNumber.s, " ", "" )
  pNumber.s = ReplaceString ( pNumber.s, "-", "" )
  
  ; Set the string length
  lNumLen.l = Len ( pNumber.s )
  
  ; Loop through each digit And do the maths
  lTotal.l = 0
  
  For lLoop.l = 0 To (lNumLen.l - 1) Step 1
  
    lDigit.l = Val(Mid(pNumber.s,(lNumLen.l - lLoop.l),1))

    ; Multiply alternate digits by two
    If ((lLoop.l % 2) = 1)
    
      lDigit.l = (lDigit.l * 2)
    
      ; If the sum is two digits, add them together (in effect)
      If (lDigit.l > 9)
        lDigit.l = (lDigit.l - 9)
      EndIf

    EndIf

    ; Total up the digits
    lTotal.l = (lTotal.l + lDigit.l)

  Next lLoop.l
  
  ; If the total mod 10 equals 0, the number is valid
  If ((lTotal.l % 10) = 0)
    lRetVal.l = 1
  EndIf
  
  ProcedureReturn lRetVal.l
EndProcedure

Debug LuhnCheck("49927398716")

Debug LuhnCheck("808401912038795")
I use this for validating NPI (National Provider ID) numbers (healthcare providers) in the United States, which is done by pre-pending "80840" onto the NPI number.

Please test with actual credit card numbers or NPI or any other Luhn-validateable number.

Comments and improvements are welcome.

Thanks.
Image Image
Wolf
Enthusiast
Enthusiast
Posts: 229
Joined: Sat Apr 03, 2004 12:00 pm
Location: S.T

GetProcAddress and problem in unicode

Post by Wolf »

Nice code thanks :wink:
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

Hi,

I also use this algorithm in some apps.
I made some times ago two procedures about Luhn:

TrueOrFalse = Luhn_IsValid(number.s)

Key = Luhn_GetKey(number.s)

Code: Select all

Procedure.l Luhn_GetKey(number.s)
  
  Protected _parity, _digit, _sum, _i = Len(number)
  
  While _i 
    _digit = Val(Mid(number, _i, 1)) 
    _parity ~ _parity 
    _sum + _digit 
    If _parity 
      _sum + _digit 
      If _digit > 4
        _sum - 9 
      EndIf 
    EndIf 
    _i - 1
  Wend 
  
  _sum % 10 
  
  If _sum 
    ProcedureReturn 10 - _sum 
  EndIf 
  
EndProcedure

Procedure.l Luhn_IsValid(number.s)
  
  Protected _parity, _digit, _sum, _i = Len(number)
  
  While _i 
    _digit = Val(Mid(number, _i, 1)) 
    _sum + _digit 
    If _parity 
      _sum + _digit 
      If _digit + _digit > 9
        _sum - 9 
      EndIf 
    EndIf 
    _parity ~ _parity 
    _i - 1
  Wend
  
  If Not _sum % 10 
    ProcedureReturn #True
  EndIf 
  
EndProcedure

Debug Luhn_GetKey("4992739871") 
Debug Luhn_GetKey("80840191203879") 

Debug Luhn_IsValid("49927398716")
Debug Luhn_IsValid("808401912038795") 

No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
bonnieya
New User
New User
Posts: 1
Joined: Mon Oct 05, 2009 9:58 am

Re: Luhn Check - Credit Card Checker Validation

Post by bonnieya »

How do you erase the credit card number off the itunes account? Ok so i was just going to use the Itunes Gift cards right.. But i dont want to use any of the money from my moms card and of course she dont want me to use her money either but, i was wondering can you erase the credit card number and how. Also can you still have yur AppleID or account still with out the credit card becuz i just want to use the itunes gify cards.
_________________
affiliateelite ~ affiliateelite.com ~ adgooroo ~ adgooroo.com
Last edited by bonnieya on Sat Oct 10, 2009 7:01 am, edited 1 time in total.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Luhn Check - Credit Card Checker Validation

Post by rsts »

bonnieya wrote:How do you erase the credit card number off the itunes account? Ok so i was just going to use the Itunes Gift cards right.. But i dont want to use any of the money from my moms card and of course she dont want me to use her money either but, i was wondering can you erase the credit card number and how. Also can you still have yur AppleID or account still with out the credit card becuz i just want to use the itunes gify cards.
With this routine or a PureBasic program?

If not, your question would be better placed in "Off topic"

cheers
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Re: Luhn Check - Credit Card Checker Validation

Post by Hroudtwolf »

Hi,

Nice implementation.
Thank you for sharing.

A few month ago, I made an implementation of creditcard ID checking via regular expressions.
http://purebasic-lounge.com/viewtopic.p ... visa#59234
Maybe not the best way, but it is another one.

Best regards
Wolf
Post Reply