信用卡验证(卢恩算法)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了信用卡验证(卢恩算法)相关的知识,希望对你有一定的参考价值。
Uses the Luhn formula to quickly validate a credit card. Basically all the digits except for the last one are summed together and the output is a single digit (0 to 9). This digit is compared with the last digit ensure a proper credit card number is entered (Does not actually confirm that is is a real number, just that it is likely to be one. Example: Entering "4000-0000-0000-0002" will pass the check, but "4000-0000-0000-0003" will not pass.)
function CheckCC(CCNo) Dim i, w, x, y y = 0 'Ensure the proper format of the input CCNo = Replace(Replace(Replace(CStr(CCNo), "-", ""), " ", ""), ".", "") 'Process digits from right to left, drop last digit if total length is even w = 2 * (Len(CCNo) Mod 2) For i = Len(CCNo) - 1 To 1 Step -1 x = Mid(CCNo, i, 1) if IsNumeric(x) Then Select Case (i Mod 2) + w Case 0, 3 'Even Digit - Odd where total length is odd (eg. Visa vs. Amx) y = y + CInt(x) Case 1, 2 'Odd Digit - Even where total length is odd (eg. Visa vs. Amx) x = CInt(x) * 2 if x > 9 Then 'Break the digits (eg. 19 becomes 1 + 9) y = y + (x 10) + (x - 10) Else y = y + x End if End Select End if Next 'Return the 10's complement of the total y = 10 - (y Mod 10) if y > 9 Then y = 0 CheckCC = (CStr(y) = Right(CCNo, 1)) End function
以上是关于信用卡验证(卢恩算法)的主要内容,如果未能解决你的问题,请参考以下文章