Skip to content

Instantly share code, notes, and snippets.

@ophentis
Created December 6, 2011 23:25
Show Gist options
  • Save ophentis/1440559 to your computer and use it in GitHub Desktop.
Save ophentis/1440559 to your computer and use it in GitHub Desktop.
TW perosnal id validator, 身份證字號檢查
//身份證字號檢查
function isValid(id) {
return typeof id === 'string' && id.match(/^[a-z][12][0-9]{8}$/i) && (function(id) {
var hash = [1, 10, 19, 28, 37, 46, 55, 64, 39, 73, 82, 2, 11, 20, 48, 29, 38, 47, 56, 65, 74, 83, 21, 3, 12, 30][id.charCodeAt(0) - 65];
for (var i = 1; i < 9; i++) {
hash += (9 - i) * (id.charCodeAt(i) - 48);
}
return (hash + id.charCodeAt(9) - 48) % 10 === 0;
})(id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment