Iranian National ID in Forms: Why Checking for '10 Digits' Isn't Enough
Iran's 10-digit national ID has a check digit. If your form only checks the length, 90% of made-up numbers slip through the filter.
Most Persian-language forms only check the length of a national ID: ten digits? Accepted. The result is that "1234567890" passes too and ends up in your database.
What a leading zero in a national ID means
The first three digits of the national ID are the code of the county (shahrestan) where it was issued, the next six are the person's serial number and the last one is the check digit. That is why most IDs issued in Tehran start with a zero, sometimes two. The zero is part of the ID: an ID that starts with zero is exactly as valid as any other, and an ID without a leading zero was simply issued in a county whose code doesn't start with zero.
The national ID has a check digit
The tenth digit of Iran's 10-digit national ID (the kod-e melli) isn't random; it is computed from the first nine. The algorithm:
- Each digit, from the left, is multiplied by a weight from 10 down to 2 (the first digit by 10, the second by 9, …, the ninth by 2).
- The products are added up and the remainder of dividing by 11 is taken.
- If the remainder is less than 2, the check digit is the remainder itself; otherwise it is 11 minus the remainder.
That means only about one in every 10 ten-digit numbers can be a valid national ID. Algorithmic validation rejects roughly 90% of random input on the spot, without connecting to any external service.
Repeated-digit codes that must be rejected
One trap: codes like "1111111111" come out as valid under the algorithm above. Any correct implementation has to reject these separately.
It has to accept Persian digits too
An Iranian user may type "۰۰۱۲۳۴۵۶۷۸" on a Persian keyboard. If the form only accepts Latin digits, the user sees an error and can't understand why, because from their point of view they typed a perfectly good number. The national ID field should automatically convert Persian and Arabic digits to Latin, not throw an error.
The same point matters when comparing: if you want each national ID to respond only once, "۱۲۳" and "123" must count as the same person. Otherwise the limit can be bypassed simply by switching keyboards.
Don't swallow the leading zero
National IDs from many cities start with a zero. If you define the field as a "number," the leading zero gets dropped in Excel and in the database, and the code becomes 9 digits. A national ID is text, not a number, even if it contains nothing but digits.
The same mistake happens with mobile numbers and bank account numbers, and it's usually discovered once you already have a thousand corrupted records.
When not to ask for a national ID at all
A national ID is sensitive identity data. If all you want is "one response per person," an employee ID or a mobile number does the same job with less risk. Ask for the national ID when you genuinely need it: contracts, official certificates, identity verification.
Frequently asked questions
What does a national ID that starts with zero mean?
The county code that forms its first three digits starts with zero, as it does for most IDs issued in Tehran. The zero is part of the ID, and the ID is fully valid.
What does an 8- or 9-digit national ID mean?
Its leading zeros were dropped somewhere, usually by Excel or a system that stored it as a number. A national ID always has 10 digits: pad it with zeros on the left (in Excel, =TEXT(A2,"0000000000")) and then validate it.
Should the form store the national ID as a number or as text?
As text. Stored as a number, the leading zeros disappear and the ID can no longer be validated or matched against other records.