Taiwan ID Number Verification
When a system requires the validation of multiple types of Taiwan identification numbers, it is common to see developers write separate APIs for National IDs, Unified ID numbers, etc. This is often because there are many ready-made examples available online, and when validation is needed, one can simply copy and paste. However, when validation examples come from different sources, it often leads to inconsistencies in naming and parameter structures. Naturally, more experienced developers will standardize these different ID validation APIs to follow the same naming conventions and interface formats.
Some may not have noticed that whether it is a "National Identification Number," "Taiwan Area Resident Certificate Number," "Alien Resident Certificate Number," "Homeless ID Number," or the "New Alien Resident Certificate Number," they all share the same check digit validation logic, with the only difference being the second digit. Therefore, the correct approach to ID validation is to first establish a common underlying logic and then pass the various ID types as parameters to validate the second digit.
Encoding Principles
The complete ID number format is as follows:
| Region Code | Gender Code | Identity Code | Sequence Code | Check Digit |
|---|---|---|---|---|
| A | 1 | 2 | 345678 | 9 |
Gender code rules:
| ID Type | Male | Female |
|---|---|---|
| National Identification Number | 1 | 2 |
| Taiwan Area Resident Certificate Number | A | B |
| Alien Resident Certificate Number | C | D |
| Homeless ID Number | Y | X |
| New Alien Resident Certificate Number | 8 | 9 |
TIP
- The fact that the Homeless ID Number uses Y for male and X for female is not a typo. It is speculated that this is due to chromosomal factors, which is why the alphabetical order does not follow the standard male-then-female sequence.
- After the "Taiwan Area Resident Certificate Number" and "Alien Resident Certificate Number" were integrated into the "New Alien Resident Certificate Number," the identity code is now used to distinguish between Taiwan residents and foreign nationals.
The English-to-number mapping is as follows:
| A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 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 |
Weighting for each digit:
| N0 | N1 | N2 | N3 | N4 | N5 | N6 | N7 | N8 | N9 | N10 |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 1 |
Handling of gender codes:
- Numbers: Use directly.
- Letters: Convert to numbers based on the mapping above, then take the last digit (units digit).
The check digit calculation formula is as follows: (N0 × 1 + N1 × 9 + N2 × 8 + N3 × 7 + N4 × 6 + N5 × 5 + N6 × 4 + N7 × 3 + N8 × 2 + N9 × 1 + N10 × 1) % 10 = 0
Example
Since I do not want to rewrite the validation code, I have provided a link to the validation code I wrote previously. If you need it, you can extract the validation portion. Code link: IdCardValidator.
Change Log
- 2023-01-16 Initial version of the document created.
