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")
Please test with actual credit card numbers or NPI or any other Luhn-validateable number.
Comments and improvements are welcome.
Thanks.