AutoHotkey Community

It is currently May 26th, 2012, 11:20 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: November 4th, 2008, 4:38 am 
Offline

Joined: December 8th, 2006, 5:17 am
Posts: 248
Location: Sydney Australia
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

_________________
Paul O


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 4th, 2008, 4:48 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Nice!

You can access the digits with a Parse loop, allowing some code simplifications:
Code:
x = 49927398716
d := 2 - (StrLen(x) & 1)
s := 0

Loop Parse, x
   s += 9<(y:=d*A_LoopField) ? y-9 : y, d := 3-d

MsgBox % mod(s,10) ? "FAIL" : "OK"


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 5th, 2008, 12:31 am 
Offline

Joined: December 8th, 2006, 5:17 am
Posts: 248
Location: Sydney Australia
WOW ! Thats all I can say.
Thanks for showing me a better way.

_________________
Paul O


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 5th, 2008, 1:17 am 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
working in this industry i shant be out done 8) ok well Lazlo thats just darn slick
here is my implementation of LUHN algorythm
poo_noo's tests some card numbers mine is an implementation of the luhn algorithm
Code:
card=3439-7500  0150.--010- #$mnopq.abcdefghijklrstuvwxyz``!@`%^&*()_+[]{}\|;:""'
removed=- .abcdefghijklmnopqrstuvwxyz``!@#$`%^&*()_+[]{}\|;:""'
loop,parse,removed
StringReplace,card,card,%A_LoopField%,,All
StringLen,cardlen,card
w:=2*Mod(cardlen, 2)
loop % cardlen - 1
{
   index:=cardlen-A_Index
   StringMid,x,card,%index%,1
   if x is Integer
   {
      i:=Mod(index, 2)+w
      y+=((i = "0") or (i = "3")) ? x : numbers(x)
   }
}
        y := 10 - Mod(y,10)
        If y > 9
      y = 0
      StringRight,c,card, 1
      StringLeft,cc,card, 1
        valid := (y = c) ? 1 : 0
        msgbox % valid ? (cc = "3" and cardlen = "15" ? "AMEX/Diners Club/JCB" : cc = "4" and cardlen = "16" ? "VISA" : cc = "5" and cardlen = "16" ? "MasterCard" : cc = "6" and cardlen = "16" ? "Discover" : "Not Card but passes LUHN") : "NONE last digit should be should be " y
           

numbers(x)
{
   i:=x*2
   loop, parse, i
      r+=A_LoopField
   Return r
}

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 7 guests


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

Search for:
Powered by phpBB® Forum Software © phpBB Group