How to Validate and Parse a Malaysian IC (NRIC) Number — with JavaScript and Python
If your app collects Malaysian IC numbers — a sign-up form, a KYC/onboarding flow,
an HR or payroll system — you can get a surprising amount of useful information
straight out of the number itself: the person's date of birth, age,
gender, and place of birth. You can also catch typos before they hit your
database. This post shows how, with working code, and where the edge cases bite.
What's inside a Malaysian IC number
A Malaysian IC (MyKad / NRIC) number has 12 digits in the form YYMMDD-PB-####:
| Part | Digits | Meaning |
|---|---|---|
YYMMDD |
1–6 | Date of birth |
PB |
7–8 | Place of birth code (state, or a country/region if born abroad) |
#### |
9–12 | Serial number; the last digit encodes gender (odd = male, even = female) |
So 900101-10-5677 decodes to: born 1 January 1990, place-of-birth code 10
= Selangor, last digit 7 (odd) = male.
Validate and parse it in JavaScript
const STATES = {
"01": "Johor", "21": "Johor", "22": "Johor", "23": "Johor", "24": "Johor",
"02": "Kedah", "25": "Kedah", "26": "Kedah", "27": "Kedah",
"03": "Kelantan", "28": "Kelantan", "29": "Kelantan",
"04": "Malacca", "30": "Malacca",
"05": "Negeri Sembilan", "31": "Negeri Sembilan", "59": "Negeri Sembilan",
"06": "Pahang", "32": "Pahang", "33": "Pahang",
"07": "Penang", "34": "Penang", "35": "Penang",
"08": "Perak", "36": "Perak", "37": "Perak", "38": "Perak", "39": "Perak",
"09": "Perlis", "40": "Perlis",
"10": "Selangor", "41": "Selangor", "42": "Selangor", "43": "Selangor", "44": "Selangor",
"11": "Terengganu", "45": "Terengganu", "46": "Terengganu",
"12": "Sabah", "47": "Sabah", "48": "Sabah", "49": "Sabah",
"13": "Sarawak", "50": "Sarawak", "51": "Sarawak", "52": "Sarawak", "53": "Sarawak",
"14": "Kuala Lumpur", "54": "Kuala Lumpur", "55": "Kuala Lumpur", "56": "Kuala Lumpur", "57": "Kuala Lumpur",
"15": "Labuan", "58": "Labuan",
"16": "Putrajaya",
};
function parseIC(input) {
const ic = String(input).replace(/[\s-]/g, "");
if (!/^\d{12}$/.test(ic)) return { valid: false, error: "Must be 12 digits" };
const [yy, mm, dd] = [ic.slice(0, 2), ic.slice(2, 4), ic.slice(4, 6)];
const pb = ic.slice(6, 8);
const lastDigit = Number(ic[11]);
// The IC does not encode the century — infer it, and never allow a future year.
const thisYear = new Date().getFullYear();
let year = 2000 + Number(yy);
if (year > thisYear) year -= 100;
const dob = new Date(year, Number(mm) - 1, Number(dd));
const realDate =
dob.getFullYear() === year &&
dob.getMonth() === Number(mm) - 1 &&
dob.getDate() === Number(dd);
const state = STATES[pb] || null; // null = born abroad or an unassigned code
return {
valid: realDate && state !== null,
dateOfBirth: realDate ? `${year}-${mm}-${dd}` : null,
gender: lastDigit % 2 === 1 ? "male" : "female",
state,
};
}
console.log(parseIC("900101-10-5677"));
// { valid: true, dateOfBirth: "1990-01-01", gender: "male", state: "Selangor" }
The same in Python
import re
from datetime import date
STATES = {
"01": "Johor", "21": "Johor", "22": "Johor", "23": "Johor", "24": "Johor",
"10": "Selangor", "41": "Selangor", "42": "Selangor", "43": "Selangor", "44": "Selangor",
"14": "Kuala Lumpur", "54": "Kuala Lumpur", "55": "Kuala Lumpur",
# ... (same full table as above)
}
def parse_ic(value: str) -> dict:
ic = re.sub(r"[\s-]", "", str(value))
if not re.fullmatch(r"\d{12}", ic):
return {"valid": False, "error": "Must be 12 digits"}
yy, mm, dd = int(ic[0:2]), int(ic[2:4]), int(ic[4:6])
pb, last = ic[6:8], int(ic[11])
year = 2000 + yy
if year > date.today().year:
year -= 100
try:
dob = date(year, mm, dd)
real = True
except ValueError:
dob, real = None, False
state = STATES.get(pb)
return {
"valid": real and state is not None,
"date_of_birth": dob.isoformat() if real else None,
"gender": "male" if last % 2 else "female",
"state": state,
}
The parts that trip people up
-
The century isn't in the number.
00could be 1900 or 2000. A good heuristic uses the serial digit (pre-2000 births usually start 5/6/7, later ones start 0/1/2/3), then falls back to "never a future year." -
There are a lot of birthplace codes. Beyond the main state codes, each state
has overflow codes, and there are codes for people born abroad (
60Brunei,66Singapore, …) and region groups. The table above is trimmed — the full one is long. - No official checksum. Malaysia's JPN does not publish a check-digit algorithm, so you can validate the format, a real calendar date, and a known birthplace code — but you cannot "verify" an IC against a government record from the number alone.
- Never store more than you need, and treat IC numbers as personal data under the PDPA.
Don't want to maintain all of that?
Keeping the full birthplace table, the century logic, and the leap-year checks
current is fiddly. If you'd rather call an endpoint and get a clean result, there's
a hosted Malaysian IC Parser & Validator API that does exactly this — stateless,
nothing stored:
GET https://micro-apis-vert.vercel.app/ic/parse?ic=900101-10-5677
{
"valid": true,
"dateOfBirth": "1990-01-01",
"age": 36,
"gender": "male",
"bornInMalaysia": true,
"placeOfBirth": { "code": "10", "type": "state", "name": "Selangor" }
}
It handles the full birthplace table (states, overflow codes, foreign and region
codes), the century inference, and leap-year validation, with a free tier to try.
Subscribe on RapidAPI: https://rapidapi.com/KiyoTech/api/malaysian-ic-parser-and-validator
Top comments (0)