统一信用代码的校验

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
  }
}

 

posted @ 2022-06-10 14:27  Stitchhhhh  阅读(549)  评论(0编辑  收藏  举报