Skip to content

Instantly share code, notes, and snippets.

@jordiup
Last active January 12, 2026 23:18
Show Gist options
  • Select an option

  • Save jordiup/a462c900ef2a41fd5afbde6ec30d01af to your computer and use it in GitHub Desktop.

Select an option

Save jordiup/a462c900ef2a41fd5afbde6ec30d01af to your computer and use it in GitHub Desktop.
Medicare Number Validation - Typescript
/**
* Australian Medicare Number Validation
*
* Validates an 11-digit Medicare card number using the official checksum algorithm.
*
* Structure (11 digits total):
* - Identifier: 8 digits (first digit must be 2-6, indicates state/territory)
* - Checksum: 1 digit (weighted sum mod 10)
* - Issue Number: 1 digit (card reissue count)
* - IRN: 1 digit (Individual Reference Number)
*
* Checksum algorithm: Multiply first 8 digits by weights [1,3,7,9,1,3,7,9], sum, mod 10
*
* @param medicare - 11-digit Medicare number (spaces/dashes allowed)
* @returns { isValid, error?, components? }
*
* @example
* validateMedicare("2951 8731 2 5 1") // { isValid: true, components: { ... } }
* validateMedicare("21234567891") // { isValid: false, error: "Invalid checksum" }
*
* @see https://doccy.com.au/medicare-number-validation
* @author Louis J Sisk, Jordi Hermoso
*/
const validateMedicare = (medicare: string) => {
const weights = [1, 3, 7, 9, 1, 3, 7, 9] as const;
const digits = medicare.replace(/\D/g, "");
if (digits.length !== 11) {
return { isValid: false, error: `Expected 11 digits, got ${digits.length}` };
}
const [identifier, checksum, issueNumber, irn] = [
digits.slice(0, 8),
digits[8],
digits[9],
digits[10],
];
const firstDigit = parseInt(identifier[0]!, 10);
if (firstDigit < 2 || firstDigit > 6) {
return { isValid: false, error: `First digit must be 2-6, got ${firstDigit}` };
}
const expectedChecksum = identifier
.split("")
.reduce((sum, d, i) => sum + parseInt(d, 10) * weights[i]!, 0) % 10;
if (expectedChecksum !== parseInt(checksum!, 10)) {
return { isValid: false, error: `Invalid checksum: expected ${expectedChecksum}, got ${checksum}` };
}
return {
isValid: true,
components: { identifier, checksum, issueNumber, irn },
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment