ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Check creditcard numbers for wellformed-ness.
Submitter: andreas kupries (other recipes)
Last Updated: 2002/08/22
Version no: 1.0
Category: Text Processing, Web programming

 

5 stars 1 vote(s)


Approved

Description:

The command below checks if the specified creditcard number is syntactically valid. This code does __not__ check if there is actually creditcard with this number. Only that the number in itself is well-formed according to the rules of the creditcard companies. See the comments preceding the code for a list of the rules.

Source: Text Source

# isluhn --
#   Checks whether a given number is a valid credit card number
# Mod 10 Rules					  
# The rules for a Mod 10 check: 			  
# The credit card number must be between 13 and 16 digits. 
#   The credit card number must start with: 	  
# 	  4 for Visa Cards 			  
# 	  37 for American Express Cards 		  
# 	  5 for MasterCards 			  
# 	  6 for Discover Cards 			  
#   If the credit card number is less then 16 digits add zeros to
#   the beginning to make it 16 digits.		  
#   Multiply each digit of the credit card number by the
#   corresponding digit of the mask, and sum the results together.
#   Once all the results are summed divide by 10, if there is no
#   remainder then the credit card number is valid.
# For a card with an even number of digits, double every odd numbered digit
# and substract 9 if the product is greater than 9. Add up all the even
# digits as well as the doubled odd digits, and the result must be a
# multiple of 10 or it's not a valid card. If a card has an odd number of
# digits, perform the same addition, doubling the even numbered digits
# instead...
# Arguments:
#   num		card num to check
# Results:
#   Returns 0/1
#
proc isluhn {cardnum} {
    regsub -all {[^0-9]} $cardnum {} cardnum
    #set cardnum [format %.16d $cardnum]
    set len [string length $cardnum]
    if {$len < 13 || $len > 16} { return 0 }
    set i -1
    set double [expr {!($len%2)}]
    set chksum 0
    while {[incr i]<$len} {
	set c [string index $cardnum $i]
	if {$double} {if {[incr c $c] >= 10} {incr c -9}}
	incr chksum $c
	set double [expr {!$double}]
    }
    return [expr {($chksum%10)==0}]
}

The license for this recipe is available here.

Discussion:



Add comment

Number of comments: 1

Variation with example, Patrick Seguin, 2004/06/17

Basically the same with the addin of the card type return value and an example at the bottom
proc isCreditCardValid {cardnum type} {
upvar $type cardtype
regsub -all {[^0-9]} $cardnum {} cardnum
set len [string length $cardnum]
if {$len
Basically the same with the addin of the card type return value and an example at the bottom
proc isCreditCardValid {cardnum type} {
upvar $type cardtype
regsub -all {[^0-9]} $cardnum {} cardnum
set len [string length $cardnum]
if {$len
Add comment



Highest rated recipes:

1. With busy cursor

2. Get widget info

3. Simplified mega-widiget ...

4. Supporting mouse wheel ...

5. Multi-character split

6. LCD Number Display

7. Check creditcard numbers ...

8. A minimal debugger

9. Socket based communicatio...

10. WSCP - showServerStatus




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.