其他分享
首页 > 其他分享> > 统一信用代码的校验

统一信用代码的校验

作者:互联网

function(trigger) {
  if (typeof (trigger) === 'undefined' || !trigger) {
    trigger = 'blur'
  }
  return {
    validator: (rule, value, callback) => {
      if (value) {
        const patrn = /^[0-9A-Z]+$/
        if ((value.length !== 18) || (patrn.test(value) === false)) {
          callback('请输入正确的统一信用代码')
        } else {
          let Ancode// 统一社会信用代码的每一个值
          let Ancodevalue// 统一社会信用代码每一个值的权重
          let total = 0
          const weightedfactors = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28]// 加权因子
          const str = '0123456789ABCDEFGHJKLMNPQRTUWXY'
          // 不用I、O、S、V、Z
          for (let i = 0; i < value.length - 1; i++) {
            Ancode = value.substring(i, i + 1)
            Ancodevalue = str.indexOf(Ancode)
            total = total + Ancodevalue * weightedfactors[i]
            // 权重与加权因子相乘之和
          }
          let logiccheckcode = 31 - total % 31
          if (logiccheckcode == 31) {
            logiccheckcode = 0
          }
          const Str = '0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,T,U,W,X,Y'
          const Array_Str = Str.split(',')
          logiccheckcode = Array_Str[logiccheckcode]

          const checkcode = value.substring(17, 18)

          if (logiccheckcode != checkcode) {
            callback('请输入正确的统一信用代码')
          } else {
            callback()
          }
        }
      }
    },
    trigger: trigger
  }
}

 

标签:const,logiccheckcode,信用,代码,校验,value,trigger,let,total
来源: https://www.cnblogs.com/angia/p/16363122.html