Hi all
I couldn't find one in the forum so I created a MOD10 checker for my work and thought I would share. Seems to work OK but I am sure that others could improve it.
Code:
; http://en.wikipedia.org/wiki/Luhn_algorithm
; http://www.academic.marist.edu/mwa/idsn.htm
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
InputBox, cardnumber, AHK CreditCard CDV checker, Please enter the CreditCard Number.`nEG 1076211283172708, , , , , , , , 1076211283172708
; cardnumber = 1076211283172708
stringsplit,digit,cardnumber,
If digit0 = 15 ; a length of 15 means AMEX
{
odd_weight=1
even_weight=2
}
Else If digit0 = 16 ; a length of 16 means MC or VISA
{
odd_weight=2
even_weight=1
}
Else ; other lengths mean not a creditcard or CBA LBX
{
msgbox, %cardnumber% NOT correct length.
Return
Exitapp
}
; set the Modulus required
mod_scheme=10
;msgbox %digit0%
; loop through the all digits (as calculated in number of substrings in Array0 of the stringsplit)
Loop %digit0%
{
If Mod( A_Index, 2 ) = 1 ; Do a MOD 2 test on the loop iteration. A remainder is 1 meaning an odd postion number
If digit%A_Index%*odd_weight < 10 ; now check to see if product is a single digit
product += digit%A_Index%*odd_weight ; add products of array 1, 3 ,5 etc. Multiple each by the variable "odd_weight"
Else
product += (digit%A_Index%*odd_weight) - 9 ; The product is 2 digits, subtract 9. IE for product 15, take off 9, ie 15 - 9 = 6 which is the same as the sum of the numbers ie 1 = 5 = 6
Else ; Do a MOD 2 test on the loop iteration. A remainder of 0 means an even postion number
If digit%A_Index%*even_weight < 10 ; now check to see if product is a single digit
product += digit%A_Index%*even_weight ; add products of array 2, 4 ,6 etc. Multiple each by the variable "even_weight"
Else
product += (digit%A_Index%*even_weight) - 9 ; The product is 2 digits, subtract 9. IE for product 15, take off 9, ie 15 - 9 = 6 which is the same as the sum of the numbers ie 1 = 5 = 6
}
; work out the remainder
remainder:= mod((product),mod_scheme)
; msgbox for debugging
msgbox, weights are %odd_weight% & %even_weight%`nMod Check is %mod_scheme%`n%product% is the sum of the products`n`nremainder = %remainder%
; now display the results
If remainder = 0
msgbox, %cardnumber% passed CDV for MC, VC, AMEX or CBA LBX
Else
msgbox, %cardnumber% Failed CDV for MC, VC, AMEX or CBA LBX
Return
Exitapp