# PureBasic Forum

 It is currently Mon Sep 16, 2019 5:24 am

 All times are UTC + 1 hour

 Page 1 of 1 [ 6 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Luhn Check - Credit Card Checker ValidationPosted: Mon Nov 03, 2008 10:02 pm
 Enthusiast

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.
;
; 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.

Thanks.

_________________

Top

 Post subject: GetProcAddress and problem in unicodePosted: Tue Nov 04, 2008 12:52 am
 Enthusiast

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

Top

 Post subject: Posted: Wed Nov 05, 2008 9:57 pm

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.

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

 Post subject: Re: Luhn Check - Credit Card Checker ValidationPosted: Wed Oct 07, 2009 7:05 am
 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.
_________________

Last edited by bonnieya on Sat Oct 10, 2009 7:01 am, edited 1 time in total.

Top

 Post subject: Re: Luhn Check - Credit Card Checker ValidationPosted: Wed Oct 07, 2009 10:21 am

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

 Post subject: Re: Luhn Check - Credit Card Checker ValidationPosted: Wed Oct 07, 2009 11:05 am

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

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 6 posts ]

 All times are UTC + 1 hour

#### Who is online

Users browsing this forum: No registered users and 7 guests

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

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite