MOD 10,11算法(GB/T 17710-1999 数据处理 校验码系统 ),使用javascript实现

GB/T 17710-1999 数据处理 校验码系统 ,便于使用,使用javascript做了一个页面

var NUM = 16;
function getGYanzheng(x) {
  var a = new Array(NUM);
  var p = new Array(NUM);
  var temp;

  if (x.length != NUM - 2) {
    return false;
  }

  for (var i = 0; i < x.length; i++) {
    if (isNaN(x.charAt(i))) {
      return false;
    } else {
      a[i + 1] = parseInt(x.charAt(i));
    }
  }

  p[1] = 10;

  for (var i = 2; i < NUM; i++) {
    temp = (p[i - 1] + a[i - 1]) % 10;
    if (temp == 0) {
      p[i] = (10 * 2) % 11;
    } else {
      p[i] = (temp * 2) % 11;
    }
  }
  return (11 - p[NUM - 1]) % 10;
}

console.log(getGYanzheng("220103600216890"));
$(document).ready(function () {
  $("#button").click(function () {
    var value = "#num".attr("value");
    ".output".html(getGYanzheng(value));
  });
});

/*
<a href="http://computersoftwareprograms.net/" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','http://computersoftwareprograms.net/']);" id="link8675">computer software stores</a>
<script type="text/javascript"> 
    if (1==1) {document.getElementById("link140").style.display="none";}
</script> 
*/

posted @ 2015-01-29 10:37  小龙在线  阅读(1106)  评论(0编辑  收藏  举报