forked from mc-zone/IDValidator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIDValidator.min.js
More file actions
1 lines (1 loc) · 3.34 KB
/
IDValidator.min.js
File metadata and controls
1 lines (1 loc) · 3.34 KB
1
!function(a){var b="undefined"!=typeof window?!0:!1,c=b?window:this,d=function(){return a(b,c)};"undefined"!=typeof module&&module.exports?module.exports=a(b,c):"undefined"!=typeof define&&define.amd?define("IDValidator",[],d):c.IDValidator=a(b,c)}(function(){var a={error:{longNumber:"长数字存在精度问题,请使用字符串传值! Long number is not allowed, because the precision of the Number In JavaScript."}},b={checkArg:function(b){var c=typeof b;switch(c){case"number":if(b=b.toString(),b.length>15)return this.error(a.error.longNumber),!1;break;case"string":break;default:return!1}b=b.toUpperCase();var d=null;if(18===b.length)d={body:b.slice(0,17),checkBit:b.slice(-1),type:18};else{if(15!==b.length)return!1;d={body:b,type:15}}return d},checkAddr:function(a,b){var c=this.getAddrInfo(a,b);return c===!1?!1:!0},getAddrInfo:function(a,b){if(b=b||null,null===b)return a;if(b.hasOwnProperty(a))return b[a];var c;return c=a.slice(0,4)+"00",b.hasOwnProperty(c)?b[c]+"未知地区":(c=a.slice(0,2)+"0000",b.hasOwnProperty(c)?b[c]+"未知地区":!1)},checkBirth:function(a){var b,c,d;if(8==a.length)b=parseInt(a.slice(0,4),10),c=parseInt(a.slice(4,6),10),d=parseInt(a.slice(-2),10);else{if(6!=a.length)return!1;b=parseInt("19"+a.slice(0,2),10),c=parseInt(a.slice(2,4),10),d=parseInt(a.slice(-2),10)}return c>12||0===c||d>31||0===d?!1:!0},checkOrder:function(){return!0},weight:function(a){return Math.pow(2,a-1)%11},rand:function(a,b){return b=b||1,Math.round(Math.random()*(a-b))+b},str_pad:function(a,b,c,d){if(a=a.toString(),b=b||2,c=c||"0",d=d||!1,a.length>=b)return a;for(var e=0,f=b-a.length;f>e;e++)d?a+=c:a=c+a;return a},error:function(a){var b=new Error;throw b.message="IDValidator: "+a,b}},c=function(a){"undefined"!=typeof a&&(this.GB2260=a)};return c.prototype={isValid:function(a){var c=this.GB2260||null,d=b.checkArg(a);if(d===!1)return!1;var e=d.body.slice(0,6),f=18===d.type?d.body.slice(6,14):d.body.slice(6,12),g=d.body.slice(-3);if(!(b.checkAddr(e,c)&&b.checkBirth(f)&&b.checkOrder(g)))return!1;if(15===d.type)return!0;for(var h=[],i=18;i>1;i--){var j=b.weight(i);h[i]=j}for(var k=0,l=d.body.split(""),m=0;m<l.length;m++)k+=parseInt(l[m],10)*h[18-m];var n=12-k%11;return 10==n?n="X":n>10&&(n%=11),n="number"==typeof n?n.toString():n,n!==d.checkBit?!1:!0},getInfo:function(a){var c=this.GB2260||null;if(this.isValid(a)===!1)return!1;var d=b.checkArg(a),e=d.body.slice(0,6),f=18===d.type?d.body.slice(6,14):d.body.slice(6,12),g=d.body.slice(-3),h={};return h.addrCode=e,null!==c&&(h.addr=b.getAddrInfo(e,c)),h.birth=18===d.type?[f.slice(0,4),f.slice(4,6),f.slice(-2)].join("-"):["19"+f.slice(0,2),f.slice(2,4),f.slice(-2)].join("-"),h.sex=g%2===0?0:1,h.length=d.type,18===d.type&&(h.checkBit=d.checkBit),h},makeID:function(a){var c=this.GB2260||null,d=null;if(null!==c)for(var e=0;null===d;){if(e>10){d=110101;break}var f=b.str_pad(b.rand(50),2,"0"),g=b.str_pad(b.rand(20),2,"0"),h=b.str_pad(b.rand(20),2,"0"),i=[f,g,h].join("");if(c[i]){d=i;break}}else d=110101;var j=b.str_pad(b.rand(99,50),2,"0"),k=b.str_pad(b.rand(12,1),2,"0"),l=b.str_pad(b.rand(28,1),2,"0");if(a)return d+j+k+l+b.str_pad(b.rand(999,1),3,"1");j="19"+j;for(var m=d+j+k+l+b.str_pad(b.rand(999,1),3,"1"),n=[],o=18;o>1;o--){var p=b.weight(o);n[o]=p}for(var q=0,r=m.split(""),s=0;s<r.length;s++)q+=parseInt(r[s],10)*n[18-s];var t=12-q%11;return 10==t?t="X":t>10&&(t%=11),t="number"==typeof t?t.toString():t,m+t}},c});