国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ASP Luhn檢驗數字算法函數 Luhn Check Digit Algorithm Fun()

2019-11-17 04:14:15
字體:
來源:轉載
供稿:網友

因為用到信用卡驗證,就找了一下,找到以下知識及函數,先普及一下知識:

Luhn 檢驗數字算法(Luhn Check Digit Algorithm,Luhn Algorithm,LUHN Formula)

Luhn 檢驗數字算法(Luhn Check Digit Algorithm),也叫做模數10公式,是一種簡單的算法,用于驗證銀行卡、信用卡號碼的有效性的算法。對所有大型信用卡公司發行的信用卡都起作用,這些公司包括美國ExPRess、護照、萬事達卡、Discover和用餐者俱樂部等。這種算法最初是在20世紀60年代由一組數學家制定,現在Luhn 檢驗數字算法屬于大眾,任何人都可以使用它。

算法:將每個奇數加倍和使它變為單個的數字,如果必要的話通過減去9和在每個偶數上加上這些值。如果此卡要有效,那么,結果必須是10的倍數。


函數:


 程序代碼
<%
    ' Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
    '
    ' This work is licensed under the Creative Commons Attribution License. To view
    ' a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
    ' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
    ' 94305, USA.

    ' Luhn (mod 10) algorithm
    function luhn(byVal input)
        dim sum
        dim odd
        dim currentDigit
        dim regEx

        input = CStr(input)
        sum = 0
        odd = CBool(len(input) mod 2)

        ' Remove any non-numeric characters.
        if not isNumeric(input) then
            set regEx = new RegExp
            regEx.Global = true
            regEx.Pattern = "/D"
            input = regEx.Replace(input, "")
            set regEx = nothing
        end if

        ' Calculate sum of digits.
        for i = 1 to len(input)
            currentDigit = cint(mid(input, i, 1))

            if odd then
                sum = sum + currentDigit
            else
                if currentDigit * 2 > 9 then
                    sum = sum + (currentDigit * 2 - 9)
                else
                    sum = sum + (currentDigit * 2)
                end if
            end if

            odd = not odd
        next

        ' Check validity.
        if sum mod 10 = 0 then
            luhn = true
        else
            luhn = false
        end if
    end function

    ' Unit test - expected result is true
    if luhn("9558821402001225732") then
        response.write "true"
    else
        response.write "false"
    end if
%>


注:卡號是百度上隨便搜的,如有不妥敬請告知。有需要此函數的朋友可以自行搜索或者拿自己卡號做測試。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄂尔多斯市| 丹凤县| 翁源县| 五峰| 潞城市| 滕州市| 灵宝市| 玉山县| 德安县| 霸州市| 乡宁县| 闵行区| 阿鲁科尔沁旗| 永新县| 赫章县| 博野县| 新竹市| 乌兰察布市| 东乌珠穆沁旗| 普陀区| 洞口县| 勐海县| 毕节市| 浪卡子县| 巴林右旗| 东台市| 松潘县| 额尔古纳市| 天水市| 金乡县| 普宁市| 宝坻区| 呼伦贝尔市| 黑龙江省| 汉寿县| 廊坊市| 霍林郭勒市| 梅州市| 东乌| 盐城市| 宁海县|