blindwig Posted April 11, 2006 Share Posted April 11, 2006 (edited) I've just written 2 functions to work with Luhn numbers. Luhn numbers are (usually) many digits long, and have a built-in checksum. This checksum allows the number to be checked against someone accidently entering it in wrong. Luhn numbers are good for account numbers (in fact the 16-digit credit card numbers issued by MasterCard and Visa are Luhn numbers)Here's the functions:expandcollapse popup;=============================================================================== ; ; Description: Returns weather a string contains a Luhn number ; Parameter(s): $s_Num is a string expected to be only digits (0-9) ; Requirement(s): None ; Return Value(s): On Success - Returns a boolean True or False ; Set @EXTENDED to the Luhn Checksum ; On Failure - Return a plain-english description of the error ; @ERROR to: 1 - Input is not a valid numeric string ; Author(s): Mike Ratzlaff AKA BlindWig ; Note(s): ; ;=============================================================================== Func _LuhnValidate($s_Num) If IsString($s_Num) And StringIsDigit($s_Num) Then Local $a_Digit = StringSplit($s_Num,'') Local $i_Count, $i_State = 0, $i_Temp, $i_CheckSum, $RetVal For $i_Count = $a_Digit[0] To 1 Step -1 If $i_State Then $i_Temp = $a_Digit[$i_Count] * 2 If $i_Temp > 9 Then $i_Temp = Int($i_temp/10) + mod($i_Temp, 10) EndIf Else $i_Temp = $a_Digit[$i_Count] EndIf $i_CheckSum += $i_Temp $i_State = Not $i_State Next $RetVal = Mod($i_Checksum,10) = 0 SetExtended($i_CheckSum) Return $RetVal Else SetError(1) Return 'Input "' & $s_Num & '" is not a valid numeric string' EndIf EndFunc ;=============================================================================== ; ; Description: Returns a string containing a Luhn number of a given length ; Parameter(s): $i_Length = number of digits for the Luhn number ; Requirement(s): Calls _LuhnValidate() ; Return Value(s): On Success - Returns a Luhn number of the given length ; On Failure - Returns '0' ; Author(s): Mike Ratzlaff AKA BlindWig ; Note(s): ; ;=============================================================================== Func _LuhnGenerate($i_Length) Local $i_Count, $s_Num, $i_CheckSum If $i_Length >= 2 Then For $i_Count = 1 To $i_Length - 1 $s_Num &= String(Random(0,9,1)) Next _LuhnValidate($s_Num & '0') $i_CheckSum = @extended $s_Num &= String(10-Mod($i_CheckSum,10)) Return $s_Num Else Return '0' EndIf EndFuncSo for example if someone gives you a creditcard number, you can verify that it's a valid account number like this:_LuhnValidate($s_GivenNumber)And if you need a random creditcard number (for example, to test an entry form) you can use this:$s_CCnum = _LuhnGenerate(16)EDITRemoved ConsoleWrite() call - that was for debugging, didn't mean to keep it in the final function Edited April 11, 2006 by blindwig My UDF Threads:Pseudo-Hash: Binary Trees, Flat TablesFiles: Filter by Attribute, Tree List, Recursive Find, Recursive Folders Size, exported to XMLArrays: Nested, Pull Common Elements, Display 2dSystem: Expand Environment Strings, List Drives, List USB DrivesMisc: Multi-Layer Progress Bars, Binary FlagsStrings: Find Char(s) in String, Find String in SetOther UDF Threads I Participated:Base64 Conversions Link to comment Share on other sites More sharing options...
Valuater Posted April 11, 2006 Share Posted April 11, 2006 Question$s_Num is a string expected to be only digits (0-9)So for example if someone gives you a creditcard number, you can verify that it's a valid account number like this:_LuhnValidate($s_GivenNumber)with those thoughts and credit cards having 16 digits, how do we use this to check a credit card number??thx8) Link to comment Share on other sites More sharing options...
greenmachine Posted April 11, 2006 Share Posted April 11, 2006 "$s_Num is a string expected to be only digits (0-9)" means you can't have "a" in your string - they can only be digits from 0 to 9. That doesn't mean the length of the string has to be from 0-9 digits long. Link to comment Share on other sites More sharing options...
Valuater Posted April 11, 2006 Share Posted April 11, 2006 "$s_Num is a string expected to be only digits (0-9)" means you can't have "a" in your string - they can only be digits from 0 to 9. That doesn't mean the length of the string has to be from 0-9 digits long.Thanks green8) Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted April 11, 2006 Moderators Share Posted April 11, 2006 This is really good! Wish you were doing this permutation code I can't get to go faster ... Great work! Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Jango Posted April 19, 2007 Share Posted April 19, 2007 I've just written 2 functions to work with Luhn numbers. Luhn numbers are (usually) many digits long, and have a built-in checksum. This checksum allows the number to be checked against someone accidently entering it in wrong. Luhn numbers are good for account numbers (in fact the 16-digit credit card numbers issued by MasterCard and Visa are Luhn numbers)Thank you for the script it's very usefull to me Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now