VendID

Developer guide

MRZ check digits: the ICAO 9303 algorithm, with JavaScript

How the check digits in a passport's machine-readable zone work, which fields they cover, and code you can paste. Try it below, or scan your own passport in the live demo.

The algorithm in four steps

  1. Give each character a value: 0–9 are 0–9, A–Z are 10–35, the filler < is 0.
  2. Multiply the values by the weights 7, 3, 1, repeating from the first character.
  3. Add the products.
  4. The check digit is the sum modulo 10.

Worked example: document number L898902C3

CharacterL898902C3
Value21898902123
Weight731731731
Product1472495627014363

Sum 316, 316 mod 10 = 6: the digit right after the number in the specimen line L898902C36UTO….

Where the check digits sit on a passport (TD3, line 2)

L898902C3 6 UTO 740812 2 F 120415 9 ZE184226B<<<<< 1 0
└ number ┘ │     └birth┘ │   └expiry┘ │ └─ optional ──┘ │ └ composite
           check        check        check             check
  • Document number, positions 1–9, check digit at 10.
  • Birth date (YYMMDD), 14–19, check at 20.
  • Expiry date (YYMMDD), 22–27, check at 28.
  • Optional data (personal number), 29–42, check at 43.
  • Composite at 44: over positions 1–10, 14–20 and 22–43.

ID cards (TD1, three lines of 30) carry the document number check on line 1, birth and expiry checks on line 2, and the composite at the end of line 2.

The JavaScript

Plain JavaScript, no dependencies. This page runs exactly this code in the calculator below.

// ICAO Doc 9303 Part 3, §4.9: check digit = sum of value × weight (7, 3, 1 repeating), modulo 10.
// Values: 0–9 are themselves, A–Z are 10–35, the filler '<' is 0.
function checkDigit(field) {
  const weights = [7, 3, 1]
  let sum = 0
  for (let i = 0; i < field.length; i++) {
    const c = field[i]
    let v
    if (c >= '0' && c <= '9') v = c.charCodeAt(0) - 48
    else if (c >= 'A' && c <= 'Z') v = c.charCodeAt(0) - 55
    else if (c === '<') v = 0
    else throw new Error('Not an MRZ character: ' + c)
    sum += v * weights[i % 3]
  }
  return sum % 10
}

// Line 2 of a passport (TD3, 44 characters): four field checks and the composite check.
function validateTd3(line2) {
  if (line2.length !== 44) return { ok: false, error: 'line 2 must be 44 characters' }
  const d = (pos) => Number(line2[pos])
  const checks = {
    document_number: checkDigit(line2.slice(0, 9)) === d(9),
    birth_date: checkDigit(line2.slice(13, 19)) === d(19),
    expiry_date: checkDigit(line2.slice(21, 27)) === d(27),
    optional_data: checkDigit(line2.slice(28, 42)) === d(42),
    composite: checkDigit(line2.slice(0, 10) + line2.slice(13, 20) + line2.slice(21, 43)) === d(43),
  }
  return { ok: Object.values(checks).every(Boolean), checks }
}

Try it

Check digit: 6

Result: all five check digits valid

Runs in your browser; nothing is sent anywhere. To read the line from a photo, use the live scanner.

Or let the API do it

The VendID MRZ API reads the MRZ from a photo and checks every digit before it answers. €0.01 per successful scan, 500 free every month; a read with a wrong digit is not billed.

"check_digits": { "document_number": true, "birth_date": true, "expiry_date": true, "composite": true }

Questions

How is an MRZ check digit calculated?

Each character gets a value (digits as themselves, A to Z as 10 to 35, the filler as 0), is multiplied by the weights 7, 3, 1 repeating, and the sum modulo 10 is the check digit (ICAO Doc 9303 Part 3).

Which fields have a check digit?

On a passport (TD3): document number, birth date, expiry date, the optional data, and a composite digit over all of them. ID cards (TD1) have the same except the optional-data digit.

Does a valid check digit prove the passport is genuine?

No. It proves the line was read without error. To prove the document is genuine, read the NFC chip and check the issuing state's signature (Passive Authentication); the VendID mobile SDK does that on the phone.

Can I skip writing this myself?

Yes. The VendID MRZ API checks every digit before it answers and returns the result per field; only reads with valid check digits are billed, €0.01 each, 500 free every month.