博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
luhn算法(转)
阅读量:6711 次
发布时间:2019-06-25

本文共 2450 字,大约阅读时间需要 8 分钟。

http://hi.baidu.com/w01fer/blog/item/93d185119a58ca0b213f2ea2.html

LUHN算法,主要用来计算信用卡等证件号码的合法性。

next section from:http://en.wikipedia.org/wiki/Luhn_algorithm

The Luhn algorithm or Luhn formula, also known as the " 10" or "mod 10" , is a simple formula used to validate a variety of identification numbers, such as , , in US and . It was created by scientist and described in , filed on January 6, 1954, and granted on August 23, 1960.

The formula verifies a number against its included check digit, which is usually appended to a partial account number to generate the full account number. This account number must pass the following test:

  1. Counting from the check digit, which is the rightmost, and moving left, double the value of every second digit.
  2. Sum the digits of the products together with the undoubled digits from the original number.
  3. If the total ends in 0 (put another way, if the total modulo 10 is equal to 0), then the number is valid according to the Luhn formula; else it is not valid.

As an illustration, if the account number is 49927398716, it will be validated as follows:

  1. Double every second digit, from the rightmost: (1×2) = 2, (8×2) = 16, (3×2) = 6, (2×2) = 4, (9×2) = 18
  2. Sum all the individual digits (digits in parentheses are the products from Step 1): 6 + (2) + 7 + (1+6) + 9 + (6) + 7 + (4) + 9 + (1+8) + 4 = 70
  3. Take the sum modulo 10: 70 mod 10 = 0; the account number is valid.

翻译过来就是:

1、从卡号最后一位数字开始,逆向将奇数位(1、3、5等等)相加。
2、将偶数位数字相加,但是这里有个麻烦。必须先将数字乘以2,如果结果是两位数,将两个位上数字相加。然后将这些结果加入总和中。
3、将奇数位总和加上偶数位总和,如果信用卡号码是合法的,结果应该可以被10整除。

现已Master Card为例:

//from:professional JavaScript for web developers

<script>

   function isValidMasterCard(sText){
    var reMasterCard=/^(5[1-5]\d{2})[\s\-]?(\d{4})[\s\-]?(\d{4})[\s\-](\d{4})$/;
    
    if(reMasterCard.test(sText)){
     var sCardNum=RegExp.$1+RegExp.$2+RegExp.$3+RegExp.$4;   
      alert(sCardNum);   
     //Luhn algorithm here
     return luhnCheckSum(sCardNum);
    }else{
     return
    }
   }
   
   function luhnCheckSum(sCardNum){
    var iOddSum=0;
    var iEvenSum=0;
    var bIsOdd=true;
    
    for(var i=sCardNum.length-1;i>=0;i--){
    //alert("length="+sCardNum.length);
    //alert("sCardNum.char("+i+")="+sCardNum.charAt(i));
     var iNum=parseInt(sCardNum.charAt(i));
     
     if(bIsOdd){
     //反向奇数求和
      iOddSum+=iNum;   
      
     }else{
      //偶数   
      if(iNum>9){
       iNum=eval(iNum.toString().split("").join("+"));
      }
      iEvenSum+=iNum;
     
     }
     bIsOdd=!bIsOdd;
    }
     return ((iEvenSum+iOddSum)%10==0);
   }
   
   alert(isValidMasterCard("5432 1234 5678 9012"));
   alert(isValidMasterCard("5432-1234-5678-9012"));
   
</script>

转载地址:http://vxalo.baihongyu.com/

你可能感兴趣的文章
深入探索JVM自动资源管理
查看>>
Steve Thair谈DevOps on Windows的演变与面临的挑战
查看>>
过去一年,被我们“高估”的技术清单
查看>>
案例学习:Jigsaw模块化迁移
查看>>
ASP.NET 2.2 Preview 1首次支持Java SignalR客户端
查看>>
Netty 源码分析之 零 磨刀不误砍柴工 源码分析环境搭建
查看>>
[deviceone开发]-动画示例源码
查看>>
实现iOS图片等资源文件的热更新化(一): 从Images.xcassets导出合适的图片
查看>>
magento2 ajax机制 (customer-data)
查看>>
magento 2模块开发实例helloworld模块
查看>>
关于if-else流程图的画法
查看>>
calc 与 box-sizing 的替代
查看>>
如何使用 Java 构建微服务?
查看>>
kafka的SSL证书校验不通过
查看>>
glom模块的使用(二)
查看>>
Spring Boot 的 10 个核心模块
查看>>
我30岁了,转行学编程可以吗? 排除法告诉你答案
查看>>
Python进阶:全面解读高级特性之切片!
查看>>
社交大战升级,语音新物种“听途听app”获数百万天使轮融资
查看>>
C#并行开发_Thread/ThreadPool, Task/TaskFactory, Parallel
查看>>