Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add zh-TW National ID Rule #1040

Merged
merged 7 commits into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Validator | Description
**isHash(str, algorithm)** | check if the string is a hash of type algorithm.<br/><br/>Algorithm is one of `['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']`
**isHexColor(str)** | check if the string is a hexadecimal color.
**isHexadecimal(str)** | check if the string is a hexadecimal number.
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`locale` is one of `['ES']` OR `'any'`. If 'any' is used, function will check if any of the locals match.<br/><br/>Defaults to 'any'.
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`locale` is one of `['ES', 'zh-TW']` OR `'any'`. If 'any' is used, function will check if any of the locals match.<br/><br/>Defaults to 'any'.
**isIP(str [, version])** | check if the string is an IP (version 4 or 6).
**isIPRange(str)** | check if the string is an IP Range(version 4 only).
**isISBN(str [, version])** | check if the string is an ISBN (version 10 or 13).
Expand Down
44 changes: 44 additions & 0 deletions lib/isIdentityCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,50 @@ var validators = {
return charsValue[char];
});
return sanitized.endsWith(controlDigits[number % 23]);
},
'zh-TW': function zhTW(str) {
var ALPHABET_CODES = {
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 sanitized = str.trim().toUpperCase();
if (!/^[A-Z][0-9]{9}$/.test(sanitized)) return false;
return Array.from(sanitized).reduce(function (sum, number, index) {
if (index === 0) {
var code = ALPHABET_CODES[number];
return code % 10 * 9 + Math.floor(code / 10);
}

if (index === 9) {
return (10 - sum % 10 - Number(number)) % 10 === 0;
}

return sum + Number(number) * (9 - index);
}, 0);
}
};

Expand Down
48 changes: 48 additions & 0 deletions src/lib/isIdentityCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,54 @@ const validators = {

return sanitized.endsWith(controlDigits[number % 23]);
},
'zh-TW': (str) => {
const ALPHABET_CODES = {
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,
};

const sanitized = str.trim().toUpperCase();

if (!/^[A-Z][0-9]{9}$/.test(sanitized)) return false;

return Array.from(sanitized).reduce((sum, number, index) => {
if (index === 0) {
const code = ALPHABET_CODES[number];

return ((code % 10) * 9) + Math.floor(code / 10);
}

if (index === 9) {
return ((10 - (sum % 10)) - Number(number)) % 10 === 0;
}

return sum + (Number(number) * (9 - index));
}, 0);
},
};

export default function isIdentityCard(str, locale = 'any') {
Expand Down
44 changes: 44 additions & 0 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,50 @@ var validators = {
return charsValue[_char];
});
return sanitized.endsWith(controlDigits[number % 23]);
},
'zh-TW': function zhTW(str) {
var ALPHABET_CODES = {
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 sanitized = str.trim().toUpperCase();
if (!/^[A-Z][0-9]{9}$/.test(sanitized)) return false;
return Array.from(sanitized).reduce(function (sum, number, index) {
if (index === 0) {
var code = ALPHABET_CODES[number];
return code % 10 * 9 + Math.floor(code / 10);
}

if (index === 9) {
return (10 - sum % 10 - Number(number)) % 10 === 0;
}

return sum + Number(number) * (9 - index);
}, 0);
}
};
function isIdentityCard(str) {
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.