Page 1 of 1

Luhn Check - Credit Card Checker Validation

Posted: Mon Nov 03, 2008 10:02 pm
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.

GetProcAddress and problem in unicode

Posted: Tue Nov 04, 2008 12:52 am
by Wolf
Nice code thanks :wink:

Posted: Wed Nov 05, 2008 9:57 pm
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") 


Re: Luhn Check - Credit Card Checker Validation

Posted: Wed Oct 07, 2009 7:05 am
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

Re: Luhn Check - Credit Card Checker Validation

Posted: Wed Oct 07, 2009 10:21 am
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

Re: Luhn Check - Credit Card Checker Validation

Posted: Wed Oct 07, 2009 11:05 am
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