Indonesian KTP (Kartu Tanda Penduduk) NIK Regex for JavaScript
/^[1-9][0-9]{1}[0-9]{2}[0-9]{2}((?:(?:[04][1-9]|[1256][0-9]|[37][01])|(?:[04][1-9]|[1256][0-9]|[37][01])))(0[1-9]|1[0-2])(\d{2})(\d{4})$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching indonesian ktp (kartu tanda penduduk) nik, ported and verified for JavaScript. 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 JavaScript project — whether you're validating in an Express middleware, a Next.js API route, or a client-side form.
Javascript Implementation
// Indonesian KTP (Kartu Tanda Penduduk) NIK
// ReDoS-safe | RegexVault — Identity & PII > National Identity Numbers
const indonesianKtpKartuTandaPendudukNikRegex = /^[1-9][0-9]{1}[0-9]{2}[0-9]{2}((?:(?:[04][1-9]|[1256][0-9]|[37][01])|(?:[04][1-9]|[1256][0-9]|[37][01])))(0[1-9]|1[0-2])(\d{2})(\d{4})$/;
function validateIndonesianKtpKartuTandaPendudukNik(input: string): boolean {
return indonesianKtpKartuTandaPendudukNikRegex.test(input);
}
// Example
console.log(validateIndonesianKtpKartuTandaPendudukNik("3171011504890001")); // trueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
3171011504890001 | 01710115040890001 |
1234015001990001 | 317101150489000 |
3273046501010001 | ABCD011504890001 |
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 JavaScript developers because especially critical in long-running Node.js event loops where a ReDoS vulnerability can block the entire process. 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
For female residents, the day of birth has 40 added (e.g., born on the 5th → day code 45). This means day codes 41-71 indicate female. NIK validation requires both format and checksum validation.
Technical Notes
NIK structure: 2-digit province code (11-99) + 2-digit city/regency + 2-digit district + 2-digit day/gender (women have +40 to day) + 2-digit month + 2-digit year + 4-digit sequence. Female DOB: day+40 encodes gender.
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