Luhn validator

Check a number with the Luhn algorithm, find its missing check digit or generate valid test numbers.

loading Luhn validator
About this tool

What is the Luhn algorithm?

The Luhn algorithm, also called mod 10, is a tiny checksum that catches typing mistakes in long numbers. Hans Peter Luhn, an IBM engineer, patented it in 1960, and it is now everywhere: payment card numbers, IMEI numbers of mobile phones, Canadian social insurance numbers, many national ID and tax numbers, and some loyalty card schemes. The last digit of such a number is not random. It is chosen so that a small calculation over all the digits comes out as a multiple of 10. If you mistype one digit, or swap two neighbouring digits, the calculation fails and a form can reject the number before anything is sent to a server.

This tool does three things. It validates a number and shows the sum it computed. It finds the missing check digit of a number you typed without its last digit. And it generates valid numbers with the prefix and length you choose, which is handy for test data. When a number looks like a payment card, it also shows the probable brand (Visa, Mastercard, American Express, Elo, Hipercard, Discover, JCB, Diners Club or UnionPay) from the first digits, as a hint only.

Everything runs in your browser. The number you type is never sent anywhere or stored. Random digits come from the browser's cryptographic generator. Be clear about what the check means: Luhn only proves that the digits are internally consistent. It does not prove that a card exists, is active or belongs to anyone. Never type a real card number into any page you do not need to; use test numbers instead.

How to use it

  1. Open the page. It loads the well-known test number 4111 1111 1111 1111 in the Validate mode, so you already see a result.
  2. Type or paste your own number. Spaces, hyphens and dots between groups are ignored. Any other character is reported, with the character named.
  3. Read the verdict line. It says whether the Luhn sum is a multiple of 10. For an invalid number it also says which last digit would have made it valid.
  4. Look at the fields below: the number in groups, the digit count, the sum, and the likely brand when the number looks like a card. Each field has a copy button.
  5. Switch to Check digit when you have a number without its last digit and need to complete it. The page shows the digit and the full number.
  6. Switch to Generate, choose the length and how many numbers you want, and optionally type the first digits (for example 4539 for a Visa-like number). Press Generate again for a new batch. Every number is valid and starts with your prefix.

Examples

A valid card test number

Input 4111 1111 1111 1111. The tool answers that the number is valid, shows a Luhn sum of 30 and the brand Visa. This number is published in payment gateway documentation as a test card.

The classic textbook number

79927398713  ->  valid (sum 70)
79927398710  ->  not valid, the last digit should be 3

This is the example used on the Wikipedia page for the algorithm. Changing a single digit makes the sum stop being a multiple of 10.

Completing a number

In Check digit mode, 7992739871 gives the check digit 3 and the complete number 79927398713. The same works for the first 14 digits of an IMEI: the 15th digit is the Luhn check digit.

Making test data

In Generate mode with prefix 5555, length 16 and 5 numbers, you get five numbers such as 5555 ... that pass the check and are recognised as Mastercard. Use them in unit tests and form validation tests, never as payment data.

Common mistakes

  • Believing a valid number is a real card. About one in ten random digit strings passes Luhn. A pass tells you the digits are consistent, nothing more. Only the issuer can tell if the account exists.
  • Typing letters or symbols. Letters such as O for zero or l for one are a classic mistake. The tool names the offending character so you can fix it.
  • Including the check digit when you want to compute it. In Check digit mode, type the number without its last digit. If you already have the whole number, use Validate.
  • Expecting a fixed length. Luhn works for any length of two digits or more. Cards use 13 to 19 digits, IMEI uses 15, but the algorithm does not enforce it.
  • Using generated numbers as real data. They are random and only pass the checksum. They are not accounts, cards or devices, and a payment system will reject them.
  • Trusting the brand hint. The brand comes from the first digits and the length. New ranges are assigned over time, so treat it as a clue.

Frequently asked questions

How does the algorithm work?

Starting from the right-most digit, leave it as it is, double the next one, leave the next, double the next, and so on. If a doubled digit is above 9, subtract 9. Add everything. If the total is a multiple of 10, the number is valid.

Does it catch every typing mistake?

No. It catches every single wrong digit and almost all swaps of two neighbouring digits. It misses the swap of 09 and 90, and some errors that involve several digits.

Is it safe to paste a card number here?

The number stays in your browser and is not sent or stored. Even so, use test numbers whenever you can. For real numbers, a validation in your own code is safer than any web page.

Can this tool check an IMEI or an ID number?

Yes, if that kind of number uses the Luhn check. IMEI does. For bank accounts in IBAN format use the IBAN validator, and for Brazilian documents see the test documents generator.

Where do the brand names come from?

From the public ranges of issuer identification numbers, the first six digits of a card. The tool recognises Visa, Mastercard (51 to 55 and 2221 to 2720), American Express, Elo, Hipercard, Discover, JCB, Diners Club and UnionPay.

How can I turn the digits into a barcode or check them in another base?

Use the barcode generator to draw a code from digits, or the number base converter to move a number between bases.

Report a problem