Free form and data tool

Iranian national ID test generator and validator

Type a 10-digit national ID to see whether its structure and check digit are valid; the calculation runs digit by digit in front of you. To test a form or software, use the “Test ID generator” tab to create IDs with a valid check digit, and to clean long lists, paste an ID column from Excel. Everything runs only in your browser and no ID is sent to any server.

Field types guide

Generate or check a national ID

Only in your browser; nothing is sent

Mode

For testing forms and software only. These IDs are random and only their check digit is valid; they may match someone’s real national ID. Never enter them in a real system (banks, registrations, purchases or other people’s forms).

Structure of the national ID: what each digit means

Short answer: a national ID is valid when it has exactly ten digits, not all of them the same, and the tenth digit equals the check digit computed from the first nine. The tool above checks all three conditions, and if a code has eight or nine digits it restores the leading zeros that were dropped.

The national ID has ten digits in three parts:

  • First three digits: the place-of-issue code, i.e. the county or office where the code was issued.
  • Next six digits: the person's unique number within that office.
  • Tenth digit: a check digit calculated from the first nine, which exposes typos.

The place-of-issue code is not necessarily the place of birth or residence; it is where the code was issued. So you cannot reliably infer someone's city from the first three digits, and this tool does not try.

National ID check-digit algorithm

  1. Multiply by the weights

    Multiply the first nine digits, from the left, by 10, 9, 8, 7, 6, 5, 4, 3 and 2.

  2. Add the products

    Add the nine products together.

  3. Remainder mod 11

    Take the remainder of that sum divided by 11.

  4. Compare with the tenth digit

    If the remainder is less than 2, the tenth digit must equal the remainder; otherwise it must equal 11 minus the remainder.

Example: code 0012345679

  • 0×10 + 0×9 + 1×8 + 2×7 + 3×6 + 4×5 + 5×4 + 6×3 + 7×2 = 112Weighted sum of the first nine digits
  • 112 mod 11 = 2Remainder mod 11
  • 11 − 2 = 9The remainder is 2 or more, so the check digit is 9 and the code is valid

Only about one in ten random 10-digit numbers passes this test. Most single-digit typos and swaps of two adjacent digits also break the check digit. Still, a valid code only means the structure is correct, not that the code actually belongs to someone.

This check does not verify identity. A code that is valid here may belong to nobody or to someone else. Matching a national ID with a name or mobile number is only possible through an official inquiry service.

What does a national ID starting with zero mean?

Many people's national IDs, including most of those issued in Tehran, start with a zero and sometimes two. These zeros are part of the place-of-issue code, and a code with leading zeros is just as valid as any other.

Trouble starts when the national ID is treated as a number. Excel strips leading zeros and shows 0012345679 as 12345679; the code becomes eight digits and fails every check. This tool pads eight- and nine-digit codes with zeros and warns you, because that is almost always what happened to them.

To prevent it, set the column format to Text in Excel before entering the codes (Format Cells, then Text). If the codes are already damaged, restore them to ten digits with =TEXT(A2,"0000000000").

Common mistakes in designing a national ID field are explained in the post What does a national ID starting with zero mean? Validating it correctly in forms.

Bulk-checking national IDs

If you have a registration list, a payroll sheet or a form export, copy the national ID column from Excel and paste it into Bulk check. Each line is checked separately and the line order, including empty lines, is preserved. Copy the result column and paste it next to the original column in Excel to filter the problem rows. Duplicate codes are counted separately.

Test national ID generator: what it creates and where to use it

The “Test ID generator” tab in the tool above creates a 10-digit number whose first nine digits are random and whose tenth digit is computed with the same check-digit algorithm, so any national ID field that only checks the check digit accepts it. Up to ten IDs are created at a time.

  • Proper use: testing the national ID field in a form, app or system you build yourself, and filling sample data in a test environment.
  • The ID is not assigned to any specific person, but it may by chance match someone’s real national ID: each first nine digits produce only one valid ID, and tens of millions of IDs have been issued.
  • Do not enter a test ID in real systems (banks, registrations, purchases, insurance or other people’s forms); using it instead of your own ID means submitting false information and may cause trouble for the real owner of that ID.

Test IDs are created only in your browser and are never sent to a server. A valid check digit means the ID is well-formed, not that an identity stands behind it.

National ID test data for developers: the safe way

If you are writing a national ID field or validation function, these practices complete safe testing alongside the test ID generator:

  1. A few edge cases, not a thousand random codes

    Test the function with a short, fixed list: one valid code, such as the teaching example 0012345679 on this page; the same code with its tenth digit changed, which must be rejected; an eight- or nine-digit code that lost its zeros, which your system should either pad or reject; ten identical digits; a code with Persian digits or hyphens; and empty input. These few cases cover every branch of the algorithm.

  2. Compute check digits only for fixtures

    If you need a few different valid codes, compute the check digit for a handful of prefixes with the four steps of the algorithm above and write the results as constants in your test code. Keep these codes in the test environment and never enter them into a real system.

  3. For load tests, switch the check off instead of generating codes

    If a load test needs thousands of unique records, turn the check-digit validation off with a setting in the test environment, or mock the validation function. Generating thousands of valid codes means generating thousands of codes that may belong to real people.

  4. Pseudonymise copies of the production database

    If you copy real data for development, replace each national ID with a keyed hash (HMAC) and a prefix such as TEST- that doesn't look like a national ID, not with a random valid code.

  5. Use an invalid code in screenshots and docs

    For screenshots, demos and guides, write a code such as 1234567890, which fails the test: its correct check digit is 1, not 0. Nobody will mistake it for a real code.

Porsino's form builder checks national IDs with the same algorithm, both in the browser and on the server; for a form that collects national IDs, see Online registration form for details.

National ID validation code in JavaScript and Python

If you're building a national ID field in your own app, these two functions check the same three rules as the tool above: ten digits, not all digits identical, and a correct check digit. Test both with the teaching code 0012345679 (valid) and 1234567890 (invalid).

JavaScript

function isValidNationalId(code) {
  if (!/^[0-9]{10}$/.test(code) || /^(\d)\1{9}$/.test(code)) return false;
  let sum = 0;
  for (let i = 0; i < 9; i++) sum += Number(code[i]) * (10 - i);
  const r = sum % 11;
  return Number(code[9]) === (r < 2 ? r : 11 - r);
}

Python

import re

def is_valid_national_id(code: str) -> bool:
    if not re.fullmatch(r"[0-9]{10}", code) or len(set(code)) == 1:
        return False
    r = sum(int(code[i]) * (10 - i) for i in range(9)) % 11
    return int(code[9]) == (r if r < 2 else 11 - r)

Before calling the function, convert Persian and Arabic digits to Latin, remove spaces and dashes, and pad eight- or nine-digit codes with leading zeros; the tool above does all of this itself. Repeat the check on the server too, because browser validation can be bypassed by tampering with the page.

Get the national ID right from the start

The best place to check a national ID is the moment it is entered, not weeks later in Excel. The National ID field in Porsino's form builder runs this same algorithm while the form is being filled in:

  • It accepts Persian and Arabic digits and stores them in a single format.
  • It keeps the code as text, so the leading zero is never lost.
  • It rejects an invalid code before submission, and the same check runs again on the server so it cannot be bypassed by tampering with the browser.
  • If each person may respond only once, set "One response per code" to the national ID question in the share settings, so the same code cannot be submitted twice from any device.

To build a registration form with a national ID, see Online registration form for details.

Frequently asked questions

How can I tell whether a national ID is valid?

Three conditions: ten digits, not all digits identical, and the tenth digit equal to the check digit. For the check digit, multiply the first nine digits from the left by 10 down to 2, add them up and take the remainder modulo 11; if it is below 2 that is the check digit, otherwise 11 minus it. The tool at the top of the page shows these steps for your code.

Do you have an online national ID generator?

Yes, for testing. Open the “Test ID generator” tab in the tool at the top of the page to create up to ten IDs with a valid check digit. They are only for testing forms and software: they may by chance match someone’s real ID, so never enter them in a real system. For load tests with thousands of records, it is better to turn off national ID validation in the test environment.

Where can I get national ID validation code for my app?

The “National ID validation code in JavaScript and Python” section on this page has two ready functions that check ten digits, non-identical digits and the check digit. Convert Persian digits to Latin before calling them, and repeat the check on the server.

Does a valid national ID mean a real one?

No. This check only shows that the code's structure and check digit are correct. A code can be valid yet belong to nobody, or to someone else. Matching a code to a person's identity is only possible through an official inquiry.

Why is a code like 1111111111 invalid?

A code whose ten digits are all the same passes the check-digit calculation, but it is almost always fake: it is what people type to get past a field. That is why most systems, including this tool, reject it separately.

My national ID has eight digits; what went wrong?

A national ID always has ten digits. An eight- or nine-digit code is almost always one whose leading zeros were stripped by Excel or another program. Add the zeros back at the start; this tool does that automatically and warns you.

Can you tell the city from the national ID?

The first three digits are the place-of-issue code, not the place of birth or residence. The lists of these codes circulating online are incomplete or outdated, so this tool does not guess the city.

Are the codes I enter stored?

No. The check runs in JavaScript on this page and no request goes to a server; the tool keeps working even if you disconnect from the internet after the page has loaded.

Does it check company national IDs too?

No. The national ID of legal entities has eleven digits and a different check algorithm. This tool only checks the ten-digit national ID of individuals.

Form-building guides

All posts

A form that won't accept a wrong national ID

Porsino's National ID field checks the check digit while the form is being filled in, accepts Persian digits and keeps the leading zero. Build your registration form for free.