Skip to content

Instantly share code, notes, and snippets.

@kaddopur
Created September 7, 2016 16:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaddopur/c204443fbde2b214abe4fb976dd01702 to your computer and use it in GitHub Desktop.
Save kaddopur/c204443fbde2b214abe4fb976dd01702 to your computer and use it in GitHub Desktop.
身分證字號檢查
function isTaiwanId(id) {
const mapping = [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];
const ID_REGEX = /^[A-Z][12]\d{8}$/i;
if (!ID_REGEX.test(id)) {
return false;
}
return `${mapping[id.toUpperCase().charCodeAt(0)-65]}${id.slice(1)}`
.split('')
.map(Number)
.reduce((sum, digit, index) => {
switch(index) {
case 0:
case 10:
return sum + digit;
default:
return sum + digit * (10 - index)
}
}, 0) % 10 === 0;
}
console.log(isTaiwanId('A123456789'));
console.log(isTaiwanId('A000000009'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment