需求:最近要做实名验证的功能,但是验证我们要验证严谨一点,参考了网上关于营业执照号码规则和一些大侠的代码的代码,总结一下.
营业执照号码规则:规则
代码:
//方法一:
function checkLicense(code){ var tip = "OK"; var pass= true; if(code.length != 18){ tip = "社会信用代码长度错误!"; pass = false; } var reg = /^([159Y]{1})([1239]{1})([0-9]{6})([0-9ABCDEFGHJKLMNPQRTUWXY]{9})([0-9ABCDEFGHJKLMNPQRTUWXY]{1})$/; if(!reg.test(code)){ tip = "社会信用代码校验错误!"; pass = false; } //不用I、O、S、V、Z var str = '0123456789ABCDEFGHJKLMNPQRTUWXY'; var ws =[1,3,9,27,19,26,16,17,20,29,25,13,8,24,10,30,28]; var codes = new Array(); var sum = 0; codes[0] = code.substr(0,code.length-1); codes[1] = code.substr(code.length-1,code.length); for(var i=0;i<codes[0].length;i++){ var Ancode = codes[0].charAt(i); var Ancodevalue = str.indexOf(Ancode); sum += Ancodevalue * ws[i]; } var indexOfc18 = 31 - (sum % 31); var c18 = str.charAt(indexOfc18); if(c18 != codes[1]){ tip = "社会信用代码有误!"; pass = false; } return {'errCode':pass, 'errMsg':tip}; }
//方法二 function checkLicense(code){ var tip = "OK"; var pass= true; var patrn = /^[0-9A-Z]+$/; //18位校验及大写校验 if ((code.length != 18) || (patrn.test(code) == false)) { alert("不是有效的统一社会信用编码!"); } else { var Ancode;//统一社会信用代码的每一个值 var Ancodevalue;//统一社会信用代码每一个值的权重 var total = 0; var weightedfactors = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28];//加权因子 var str = '0123456789ABCDEFGHJKLMNPQRTUWXY'; //不用I、O、S、V、Z for (var i = 0; i < code.length - 1; i++) { Ancode = code.substring(i, i + 1); Ancodevalue = str.indexOf(Ancode); total = total + Ancodevalue * weightedfactors[i]; //权重与加权因子相乘之和 } alert(total); var logiccheckcode = 31 - total % 31; if (logiccheckcode == 31) { logiccheckcode = 0; } var 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"; var Array_Str = Str.split(','); logiccheckcode = Array_Str[logiccheckcode]; alert(logiccheckcode); var checkcode = code.substring(17, 18); if (logiccheckcode != checkcode) { pass = false; tip = "不是有效的统一社会信用编码!"; alert("不是有效的统一社会信用编码!"); } } return {'errCode':pass, 'errMsg':tip}; }