Utility Coder
Back to Blog
utility

Luhn

Andy Pham
luhn, luhn, luhn online

Ready to try Luhn?

Access the free online tool now - no registration required

Open Tool

Luhn - Complete Guide

Luhn algorithm validator checks if numbers pass the Luhn checksum, commonly used for credit cards, IMEI numbers, and identification numbers. Also generates valid Luhn numbers by calculating the correct check digit for any numeric sequence.

How It Works

From rightmost digit, double every second digit. If doubled value > 9, subtract 9. Sum all digits. Valid if sum ends in 0 (mod 10 = 0). For check digit generation: calculate sum with placeholder, find digit that makes sum divisible by 10.

Technical Details

Luhn algorithm (mod 10): 1) Double alternating digits from right, 2) If result > 9, subtract 9 (or sum digits), 3) Sum all digits, 4) Valid if total % 10 === 0. Detects single digit errors and adjacent transpositions. Not cryptographically secure.

Common Use Cases

  1. Credit Card Validation: Verify card numbers before processing.
    • Example: 4111111111111111 (valid Visa test)
  2. IMEI Validation: Check mobile device identifiers.
  3. ID Number Generation: Generate valid test numbers.
  4. Data Quality: Validate numeric identifiers in databases.

Code Examples

Luhn Algorithm Implementation

// Validate a number using Luhn algorithm
function luhnValidate(number) {
  const digits = String(number).replace(/\D/g, '').split('').map(Number);
  let sum = 0;

  for (let i = digits.length - 1; i >= 0; i--) {
    let digit = digits[i];

    // Double every second digit from right
    if ((digits.length - i) % 2 === 0) {
      digit *= 2;
      if (digit > 9) digit -= 9;
    }

    sum += digit;
  }

  return sum % 10 === 0;
}

// Generate check digit
function luhnCheckDigit(number) {
  const digits = String(number).replace(/\D/g, '').split('').map(Number);
  digits.push(0); // Placeholder for check digit

  let sum = 0;
  for (let i = digits.length - 1; i >= 0; i--) {
    let digit = digits[i];

    if ((digits.length - i) % 2 === 0) {
      digit *= 2;
      if (digit > 9) digit -= 9;
    }

    sum += digit;
  }

  return (10 - (sum % 10)) % 10;
}

// Generate complete valid number
function generateLuhnNumber(prefix, length) {
  let number = prefix;

  // Fill with random digits (leaving space for check digit)
  while (number.length < length - 1) {
    number += Math.floor(Math.random() * 10);
  }

  // Add check digit
  number += luhnCheckDigit(number);

  return number;
}

// Examples
console.log(luhnValidate('4111111111111111')); // true (Visa test)
console.log(luhnValidate('4111111111111112')); // false
console.log(luhnCheckDigit('411111111111111')); // 1
console.log(generateLuhnNumber('4', 16));       // Random valid Visa-like

Complete Luhn implementation: validation, check digit calculation, and number generation.

Tips & Best Practices

  • Strip non-digits before validation
  • Luhn is for error detection, not security
  • Use official test numbers for development
  • Combine with format validation (length, prefix)
  • Check digit is always last digit

Common Mistakes to Avoid

  • Thinking Luhn proves card validity
  • Not stripping spaces/dashes from input
  • Wrong doubling direction (must be from right)
  • Forgetting to subtract 9 when doubled > 9
  • Using Luhn for security purposes

When to Use This Tool

Use Luhn validator to check credit card numbers, IMEI, or other Luhn-protected identifiers before processing. Use generator for test data only.

Frequently Asked Questions

Does Luhn validation prove a card is real?

No. Luhn only checks mathematical validity. A valid Luhn number could still be invalid (expired, wrong issuer, fake). Always verify with payment processor.

What numbers use Luhn?

Credit/debit cards (Visa, MC, Amex, Discover), IMEI numbers, Canadian Social Insurance Numbers, some national IDs. Each has additional format rules beyond Luhn.

Can I generate valid credit card numbers?

Technically yes, Luhn generates valid checksums. But using fake numbers for fraud is illegal. Test numbers (4111111111111111) are provided by payment processors for development.

What errors does Luhn catch?

Single digit errors (99.9%), adjacent transpositions (97.8%). Doesn't catch: all transpositions, twin errors (09↔90). Not designed for security, just error detection.

Get Started

Ready to try Luhn? Use the tool now - it's free, fast, and works right in your browser.


Last updated: June 27, 2026

Start using Luhn

Free, fast, and privacy-focused - try it now

Launch Tool