|
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
proc isluhn {cardnum} {
regsub -all {[^0-9]} $cardnum {} 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:
|