REGEXVAULT
Identity & PII/National Identity Numbers
ReDoS Checked

Indonesian KTP (Kartu Tanda Penduduk) NIK Regex for Java

/^[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})$/

All five implementations are free. Switch engines without signing in.

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 Java. 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 Java project — whether you're validating in a Spring Boot controller, a Jakarta EE service, or a standalone utility class.

Java Implementation

Java
// Indonesian KTP (Kartu Tanda Penduduk) NIK
// ReDoS-checked | RegexVault — Identity & PII > National Identity Numbers

import java.util.regex.Pattern;

public class IndonesianKtpKartuTandaPendudukNikValidator {
    private static final Pattern PATTERN =
        Pattern.compile("^[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})$");

    public static boolean validate(String input) {
        return PATTERN.matcher(input).matches();
    }

    // Example
    public static void main(String[] args) {
        System.out.println(validate("3171011504890001")); // true
    }
}

Test Cases

Matches (Valid)
Rejects (Invalid)
317101150489000101710115040890001
1234015001990001317101150489000
3273046501010001ABCD011504890001

When to use this pattern

This pattern is drawn from the Identity & PII > National Identity Numbers category and has been checked for ReDoS risk. That matters for Java developers because critical in Java applications since the JVM regex engine uses backtracking and is susceptible to ReDoS without careful pattern design. 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. Submissions join the review queue after automated validation.

Submit a Pattern