Hong Kong HKID Number Regex for PHP
/^([A-Z]{1,2})([0-9]{6})\(([0-9A])\)$/iWhat this pattern does
This page provides a well-structured, multi-part regular expression for matching hong kong hkid number, ported and verified for PHP. Identity and credential patterns need both correctness and safety, since they're frequent targets for adversarial input. The snippet below is ready to drop into your PHP project — whether you're validating in a Laravel validator, a WordPress plugin, or a standalone PHP script.
Php Implementation
<?php
// Hong Kong HKID Number
// ReDoS-safe | RegexVault — Identity & PII > National Identity Numbers
define('HONG_KONG_HKID_NUMBER_PATTERN', '/^([A-Z]{1,2})([0-9]{6})\(([0-9A])\)$/');
function validate_hong_kong_hkid_number(string $input): bool {
return (bool) preg_match(HONG_KONG_HKID_NUMBER_PATTERN, $input);
}
// Example
var_dump(validate_hong_kong_hkid_number("A123456(7)")); // bool(true)Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
A123456(7) | A1234567 |
AB123456(3) | A12345(7) |
Z999999(A) | A1234567(7) |
A123456(0) | 123456(7) |
| — | A123456(B) |
When to use this pattern
This pattern is drawn from the Identity & PII > National Identity Numbers category and carries a ReDoS-safe certification. That matters for PHP developers because especially relevant in PHP where PCRE backtracking limits can trigger silent failures on malicious input. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
Check character A represents 10 in the checksum. One-letter prefixes (A-Z) are issued chronologically; two-letter prefixes (AA-ZZ) started when the single-letter range was exhausted. Some older cards use lowercase or different formats.
Technical Notes
Structure: 1-2 uppercase letters + 6 digits + check character (0-9 or A) in parentheses. A single letter prefix is padded with a space for checksum calculation. The check character is computed via weighted sum mod 11.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern