MRZ Check Digit Calculation with 7-3-1 Weights
How passport and ID card MRZ check digits work, with character values, the 7-3-1 weighting, and Python code for validation.
Character values
Digits keep their value, letters A–Z map to 10–35, and the filler < counts as 0.
Extracting It Automatically
When you read this data from photos, the Passport OCR API returns it as structured JSON, and you can apply the rules on this page as validation. Try it with the free scanner.
Where This Fits
- Weighting: Multiply each character’s value by the repeating weights 7, 3, 1, add the products, and take the remainder after dividing by 10. That remainder is the check digit.
- Which fields have check digits: On a TD3 passport, the document number, date of birth, and expiry date each have a check digit, and a composite check digit covers several fields together.
Python Implementation
A minimal, dependency-free implementation you can use to validate OCR output.
Prerequisite: Python 3.9+
def mrz_check_digit(field: str) -> int:
total = 0
for i, char in enumerate(field):
if char.isdigit():
value = int(char)
elif char.isalpha():
value = ord(char.upper()) - 55
else:
value = 0 # "<" filler
total += value * (7, 3, 1)[i % 3]
return total % 10
print(mrz_check_digit("L898902C3")) # ICAO sample document number -> 6Technical Specs
- •Endpoint: POST https://api.structocr.com/v1/passport
- •Authentication: x-api-key request header
- •Inputs: JPG, PNG, or WebP as Base64 JSON; maximum 4.5MB
- •Price: 2 credits per successful request; failed requests are not billed
- •Free trial: 200 free credits on sign-up, no credit card required
Key Features
- •Weighting: Multiply each character’s value by the repeating weights 7, 3, 1, add the products, and take the remainder after dividing by 10. That remainder is the check digit.
- •Which fields have check digits: On a TD3 passport, the document number, date of birth, and expiry date each have a check digit, and a composite check digit covers several fields together.
- •Validate after OCR: Apply these rules to API output before storing it.
Reference Summary
Key facts from this page in structured form, for quick lookup.
{
"Character values": "Digits keep their value, letters A–Z map to 10–35, and the filler < counts as 0..",
"Weighting": "Multiply each character’s value by the repeating weights 7, 3, 1, add the products, and take the remainder after dividing by 10.",
"Which fields have check digits": "On a TD3 passport, the document number, date of birth, and expiry date each have a check digit, and a composite check digit covers several fields together.."
}Frequently Asked Questions
Why do check digits matter for OCR?
A mismatch shows that a character was misread, so the application can ask for a better image before storing data.
Do all documents use the same algorithm?
Yes. ICAO Doc 9303 defines the same 7-3-1 method for passports, ID cards, and visas.
You May Also Like
Related tutorials, platform guides, and comparisons
Norway Passport OCR with Node.js SDK
Use the official StructOCR Node.js SDK with TypeScript and Express to extract structured MRZ and VIZ data from Norway passports.
Greece Passport OCR with Node.js SDK
Use the official StructOCR Node.js SDK with TypeScript and Express to extract structured MRZ and VIZ data from Greece passports.
Algeria Passport OCR with Node.js SDK
Use the official StructOCR Node.js SDK with TypeScript and Express to extract structured MRZ and VIZ data from Algeria passports.
Switzerland Passport OCR with Node.js SDK
Use the official StructOCR Node.js SDK with TypeScript and Express to extract structured MRZ and VIZ data from Switzerland passports.
Ecuador Passport OCR: Extract Fields with Python
Extract visible fields from Ecuador passport images into JSON. Map identifiers, names and dates, with a review workflow for incomplete results.
Tajikistan Passport OCR with Node.js SDK
Use the official StructOCR Node.js SDK with TypeScript and Express to extract structured MRZ and VIZ data from Tajikistan passports.
Tanzania Passport OCR with Node.js SDK
Use the official StructOCR Node.js SDK with TypeScript and Express to extract structured MRZ and VIZ data from Tanzania passports.
Tajikistan Passport OCR with Python SDK
Use the official StructOCR Python SDK and FastAPI to extract structured MRZ and VIZ data from Tajikistan passports with a server-side integration.
Switzerland Passport OCR with Python SDK
Use the official StructOCR Python SDK and FastAPI to extract structured MRZ and VIZ data from Switzerland passports with a server-side integration.
MRZ Formats Explained: TD1, TD2, and TD3
The machine readable zone layouts used on passports and ID cards: line counts, line lengths, field positions, and where each format appears.
Technical Comparisons & Integrations
Explore platform integrations and competitive analysis
Add Passport OCR to Your Lovable.dev App in 5 Minutes
Step-by-step guide to integrating the StructOCR Passport scanner API into a Lovable.dev app through a secure server-side function for travel and KYC flows.
Add Passport OCR to Your Replit App in 5 Minutes
Step-by-step guide to integrating the StructOCR Passport scanner API into a Replit application. Build travel, hospitality, and global KYC flows using Replit Agent and Secrets.
Tesseract & PassportEye vs. StructOCR Passport API
A technical evaluation for developers deciding between open-source MRZ scanners and a dedicated cloud-based Passport OCR API.
Add Passport OCR to Your v0 App in 5 Minutes
Step-by-step guide to integrating the StructOCR Passport scanner API into a v0 by Vercel app through a secure Next.js server route.
AWS Textract vs. StructOCR API for Indian Passport Back Page Extraction
Why generic OCR fails at parsing the back page of Indian passports, and how a dedicated KYC API instantly extracts structured addresses, PIN codes, and family details.
AWS Textract & Google Vision vs. StructOCR Passport API
How AWS Textract AnalyzeID, Google Cloud Vision OCR, and the StructOCR Passport API differ for passport data, quoting each vendor’s own documentation.
From tutorial to production in 5 minutes.
You've seen the code. Now get your API key, grab your 200 free credits, and see it work with your own images. No credit card required.