Skip to content

Instantly share code, notes, and snippets.

@tony1223
Created October 20, 2015 13:53
Show Gist options
  • Save tony1223/ac9aec20561112e6e43c to your computer and use it in GitHub Desktop.
Save tony1223/ac9aec20561112e6e43c to your computer and use it in GitHub Desktop.
function identify(check){
var type = {a:10, b:11, c:12, d:13, e:14, f:15, g:16, h:17, j:18, k:19, l:20, m:21, n:22, p:23, q:24, r:25, s:26, t:27, u:28, v:29, w:32, x:30, y:31, z:33, i:34, o:35 };
var val = type[check[0].toLowerCase()] + check.substring(1);
var ranks = [1,9,8,7,6,5,4,3,2,1];
var checknum = 0;
for(var i = 0 ; i < ranks.length;++i){
checknum += parseInt(val[i],10) * ranks[i];
console.log(val[i],ranks[i],checknum);
}
var checkcode = check.substring(9,10);
if(checknum%10 == 0 && checkcode == 0){
return true;
}
return checkcode == (10 - checknum%10);
};
identify("身份證字號");
@wang-steven
Copy link

// without maintain
function identify(check) {
var type = [ 10, 11, 12, 13, 14, 15, 16, 17, 34, 18, 19, 20, 21, 22, 35, 23, 24, 25, 26, 27, 28, 29, 32, 30, 31, 33 ];
var i = 0, offset = 48, length = base = 10, a = 65, endIndex = 9, before = 1;
if (!!check && check.trim().length === length) {
check = check.trim().toUpperCase();
var checknum = type[check.charCodeAt(i) - a] / base;
checknum += type[check.charCodeAt(i++) - a] % base * (base - i++);
for (; i <= endIndex; checknum += (check.charCodeAt(i - before) - offset) * (base - i++));
return (check.charCodeAt(endIndex) - offset) === (base - (checknum % base));
}
return false;
}
identify("身份證字號");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment