js-纳税人识别码验证



(具体脚本方法参考网上综合所得)

//(15位包括地区编码6位+组织机构代码9位)
   var taxpayerId = $("#taxpayerId").val();
   //纳税人识别号校验是否合法
   if($.trim(taxpayerId) == ''){
    alert("请输入纳税人识别号 !");
    return;
   }else if($.trim(taxpayerId) != ''){
     var addressCode = taxpayerId.substring(0,6);
     // 校验地址码
     var check = checkAddressCode(addressCode);
     if(!check) {
      alert("请输入正确的纳税人识别号 (地址码)!");
      return;
     }else{
      // 校验组织机构代码
      var orgCode = taxpayerId.substring(6,9);
      check = orgcodevalidate(orgCode);
      if(!check){
       alert("请输入正确的纳税人识别号(组织机构代码) !");
       return;
         }
     }
   }

// 校验组织机构代码
  function orgcodevalidate(value){
    if(value!=""){
     var part1=value.substring(0,8);
     var part2=value.substring(value.length-1,1);
     var ws = [3, 7, 9, 10, 5, 8, 4, 2];
     var str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
     var reg = /^([0-9A-Z]){8}$/;
     if (!reg.test(part1))
     {
      return true
     }
     var sum = 0;
     for (var i = 0; i< 8; i++)
     {
      sum += str.indexOf(part1.charAt(i)) * ws[i];
     }
     var C9 = 11 - (sum % 11);
     var YC9=part2+'';
     if (C9 == 11) {
      C9 = '0';
     } else if (C9 == 10) {
      C9 = 'X' ;
     } else {
      C9 = C9+'';
     }
     return YC9!=C9;
   }
  }
  
  
  // 校验地址码
  function checkAddressCode(addressCode){
      var provinceAndCitys={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",
       31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",
       45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",
       65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"};
      var check = /^[1-9]\d{5}$/.test(addressCode);
      if(!check) return false;
      if(provinceAndCitys[parseInt(addressCode.substring(0,2))]){
        return true;
      }else{
        return false;
      }
      
  }


你可能感兴趣的:(项目记事)