PHP Function for Validating Chinese ID Card Numbers
This article explains the structure of Chinese citizen ID numbers and provides a PHP function that validates both 15‑digit and 18‑digit IDs by checking format, birthdate correctness, and computing the checksum according to ISO 7064, accompanied by example usage.
Chinese citizen ID numbers consist of a 17‑digit body code and a final checksum digit. The body code is composed of a six‑digit address code, an eight‑digit birthdate (YYYYMMDD), a three‑digit sequence code (odd for males, even for females), and the checksum digit.
The validation process includes:
Verifying the overall format with regular expressions for 15‑digit and 18‑digit IDs.
Extracting and checking the birthdate part using strtotime to ensure it represents a real date.
For 18‑digit IDs, calculating the checksum according to ISO 7064:1983.MOD 11‑2, where the weighting factors are 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 and the possible check characters are 0‑9 and X.
The following PHP function implements these steps:
Example usage:
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.