Taiwan ID Number Validation
When a system needs to validate multiple types of Taiwan identification numbers, it is common to see different APIs written for National IDs, Unified ID numbers, etc. This is because there are many ready-made examples available online, and if validation is needed, one can simply find and copy one. However, when the sources of these validation examples differ, it often leads to inconsistencies in naming and parameters. Of course, those with more sense will adjust different ID validation APIs to follow the same naming conventions and interface formats.
Some people may not have noticed that whether it is a "National Identification Number," "Taiwan Area Resident Certificate Unified ID Number," "Foreign Resident Certificate Unified ID Number," "Homeless ID Number," or "New Foreigner Unified ID Number," they all share the same check digit validation logic, with the only difference being the second digit. Therefore, the correct way to validate ID numbers is to first establish a common underlying layer 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 | Serial Number | Check Digit |
|---|---|---|---|---|
| A | 1 | 2 | 345678 | 9 |
Gender Code Rules:
| ID Type | Male | Female |
|---|---|---|
| National Identification Number | 1 | 2 |
| Taiwan Area Resident Certificate Unified ID Number | A | B |
| Foreign Resident Certificate Unified ID Number | C | D |
| Homeless ID Number | Y | X |
| New Foreigner Unified ID Number | 8 | 9 |
TIP
- The Homeless ID Number using Y for male and X for female is not a typo. It is speculated that this is due to chromosomal reasons, so the alphabetical order does not follow the male-first convention.
- After the "Taiwan Area Resident Certificate Unified ID Number" and "Foreign Resident Certificate Unified ID Number" were integrated into the "New Foreigner Unified ID 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 the Gender Code:
- Numeric: Use directly.
- Alphabetical: Convert to a number based on the mapping above, then take the last 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 containing 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 document creation.