Skip to content

Instantly share code, notes, and snippets.

@wildskyf
Created January 15, 2019 07:19
Show Gist options
  • Save wildskyf/c1926b864c64784d230bb7a0aa8a7a18 to your computer and use it in GitHub Desktop.
Save wildskyf/c1926b864c64784d230bb7a0aa8a7a18 to your computer and use it in GitHub Desktop.
Taiwan National Identification Number Validator 台灣身分證驗證
function isValidIdNumber(str) {
if(!/^[A-Z]\d{9}$/.test(str)) return false;
var multiplier = [1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1];
var map = {
'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'I': 34,
'J': 18, 'K': 19, 'L': 20, 'M': 21, 'N': 22, 'O': 35, 'P': 23, 'Q': 24, 'R': 25,
'S': 26, 'T': 27, 'U': 28, 'V': 29, 'W': 32, 'X': 30, 'Y': 31, 'Z': 33
};
var decoder = function(id_number){
return id_numbar.replace(/[A-Z]/, function(char){ return map[char]; });
};
var numbers = decoder(str).split(), sum = 0;
for (var i = 0 ; i < 11 ; i++) {
sum += numbers[i] * multiplier[i];
}
return sum % 10 == 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment