Malaysian IC Number (MyKad / NRIC) Regex for Java
/^(\d{2})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])-(0[0-9]|1[0-6])-(\d{4})$/What this pattern does
This page provides a well-structured, multi-part regular expression for matching malaysian ic number (mykad / nric), 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
// Malaysian IC Number (MyKad / NRIC)
// ReDoS-safe | RegexVault — Identity & PII > National Identity Numbers
import java.util.regex.Pattern;
public class MalaysianIcNumberMykadNricValidator {
private static final Pattern PATTERN =
Pattern.compile("^(\\d{2})(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])-(0[0-9]|1[0-6])-(\\d{4})$");
public static boolean validate(String input) {
return PATTERN.matcher(input).matches();
}
// Example
public static void main(String[] args) {
System.out.println(validate("880101-14-5555")); // true
}
}Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
880101-14-5555 | 880101145555 |
990231-10-1234 | 880101-17-5555 |
010115-01-0001 | 880001-14-5555 |
001231-12-9999 | 880132-14-5555 |
| — | 8801014-14-555 |
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 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
The MyKad encodes date of birth and gender directly in the number — exposure of the IC number reveals both. Foreigners use a different format with different state codes (60-93 for foreign workers).
Technical Notes
Structure: YYMMDD (date of birth) + state code (01-16) + sequential number. Last digit odd=male, even=female. State codes: 01=Johor, 02=Kedah, ..., 14=Federal Territory, 15=Sabah, 16=Sarawak. YYMMDD does not validate day-of-month correctness (e.g., 990231 is Feb 31).
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