It is currently Sat Jan 19, 2019 1:11 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Luhn Check - Credit Card Checker Validation
PostPosted: Mon Nov 03, 2008 10:02 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Apr 13, 2005 10:45 pm
Posts: 701
Location: Idaho, USA
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:
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


Top
 Profile  
Reply with quote  
 Post subject: GetProcAddress and problem in unicode
PostPosted: Tue Nov 04, 2008 12:52 am 
Offline
Enthusiast
Enthusiast

Joined: Sat Apr 03, 2004 12:00 pm
Posts: 220
Location: S.T
Nice code thanks :wink:


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Nov 05, 2008 9:57 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Jul 22, 2003 5:02 pm
Posts: 1534
Location: In a long distant galaxy
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Luhn Check - Credit Card Checker Validation
PostPosted: Wed Oct 07, 2009 7:05 am 
Offline
New User
New User

Joined: Mon Oct 05, 2009 9:58 am
Posts: 1
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.

Top
 Profile  
Reply with quote  
 Post subject: Re: Luhn Check - Credit Card Checker Validation
PostPosted: Wed Oct 07, 2009 10:21 am 
Offline
Addict
Addict

Joined: Wed Aug 24, 2005 8:39 am
Posts: 2736
Location: Southwest OH - USA
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Luhn Check - Credit Card Checker Validation
PostPosted: Wed Oct 07, 2009 11:05 am 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 12, 2005 3:35 am
Posts: 803
Location: Germany(Hessen)
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.php?p=59234&highlight=visa#59234
Maybe not the best way, but it is another one.

Best regards
Wolf

_________________
German blog about IT and programming
German PB forums


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: Majestic-12 [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye