Taiwan ID Number Validation
TLDR
- The validation logic for check digits in various Taiwan ID numbers (National ID, Resident Certificate, Homeless ID, etc.) is identical.
- A unified underlying validation logic should be established instead of writing separate APIs for different ID types to simplify maintenance costs.
- The core difference in ID validation lies solely in the mapping rules for the "second digit (gender code)."
- When converting English codes to numbers, a specific mapping table must be referenced, and certain letters (such as I, O, W, X, Y, Z) have special conversion values.
Importance of Unified Validation Logic
When you encounter this: When a system needs to support validation for multiple ID types simultaneously, such as National ID, Resident Certificate, and Homeless ID.
If you write separate validation APIs for each ID type, it often leads to naming confusion, inconsistent parameter formats, and extremely high maintenance costs. In fact, the check digit calculation formula for all the aforementioned ID numbers is the same; the only difference is the definition of the second digit (gender code). It is recommended to create a shared underlying validation function that encapsulates the gender code rules for different ID types as parameters, which will significantly simplify the code.
Encoding Principles and Calculation Methods
The complete ID format includes the area code, gender code, identity code, sequence number, and check digit.
Gender Code Mapping Table
When you encounter this: When processing different types of ID numbers, you need to correctly identify the definition of the gender code.
| ID Type | Male | Female |
|---|---|---|
| National ID | 1 | 2 |
| Taiwan Area Resident Certificate | A | B |
| Foreign Resident Certificate | C | D |
| Homeless ID | Y | X |
| New Foreigner Resident Certificate | 8 | 9 |
TIP
- The Male Y and Female X for Homeless IDs are not typos; it is speculated to be related to chromosomal encoding logic.
- The new Foreigner Resident Certificate has integrated the old resident certificate types and uses the identity code to distinguish regions.
Conversion and Calculation Rules
When you encounter this: When you need to convert English codes into numbers before performing check digit calculations.
- English to Number Mapping Table:
- 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.
- Gender Code Processing:
- Numbers: Use directly.
- English: Convert to a number, then take the last digit.
- Weighting:
- The weights for positions N0 to N10 are: 1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1.
- Check Digit Formula:
(N0 × 1 + N1 × 9 + N2 × 8 + N3 × 7 + N4 × 6 + N5 × 5 + N6 × 4 + N7 × 3 + N8 × 2 + N9 × 1 + N10 × 1) % 10 = 0
Implementation Suggestions
When you encounter this: When you need to quickly implement standardized validation logic.
To avoid reinventing the wheel, it is recommended to refer to standard validation implementations. Below is a reference link for C# validation logic, from which developers can extract the core validation logic for encapsulation:
Change Log
- 2023-01-16 Initial document creation.
