1 function fill(CODE)//CODE是要校验的组织机构代码 2 { 3 4 if (CODE.length != 9) 5 { 6 alert("组织机构代码位数不正确"); 7 8 } 9 else10 {11 var patrn = /^[0-9A-Z]+$/;12 13 if (patrn.test(CODE) == false)14 {15 16 alert("组织机构代码只可为数字或大写拉丁字母");17 18 }19 else20 {21 var lastpatrn = /^[0-9X]+$/;22 23 var checkcode = CODE.substring(8, 9);24 25 if (lastpatrn.test(checkcode) == false)26 {27 28 alert("组织机构代码最后一位只可为数字或大写拉丁字母:X");29 }30 else31 {32 var ancode;33 var ancodevalue;34 var total = 0;35 var weightedfactors = [3, 7, 9, 10, 5, 8, 4, 2];//加权因子36 var str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';37 for (var i = 0; i < CODE.length - 1; i++)38 {39 40 ancode = CODE.substring(i, i + 1);41 ancodevalue = str.indexOf(ancode);42 total = total + ancodevalue * weightedfactors[i];43 }44 var logiccheckcode = 11 - total % 11;45 if (logiccheckcode == 10)46 logiccheckcode = 'X';47 if (logiccheckcode == 11)48 logiccheckcode = '0';49 if (checkcode != logiccheckcode)50 {51 52 alert("组织机构代码结构错误.最后一位校验码应为:" + logiccheckcode);53 }54 else55 {56 alert("组织机构代码正确);57 }58 }59 }60 }61 } //代码的大致架子来自网络,本人做了修改。如有原创人员看到此代码,请联系我注明标注出处 //转载请注明出处