DevToolBox

Luhn Algorithm Checker

Luhnアルゴリズム(mod 10)で数字列を検証し、チェックディジットも生成できます。処理はブラウザ内で完結します。

重要 / Important

この検証は数字列がLuhnアルゴリズムの計算規則を満たすかだけを確認します。実在するクレジットカード・デビットカード等の有効性、利用可否、発行状況を確認・保証するものでは一切ありません。実在するカード番号を入力する必要はなく、テスト用のダミー数字列で十分です。

This checker only tests whether digits satisfy the Luhn calculation. It never verifies or guarantees that a credit/debit card exists, is issued, or can be used. Do not enter a real card number; dummy test digits are sufficient.

空白とハイフンは自動的に除去されます。 / Spaces and hyphens are ignored.

How to Use

Select Validate when the final digit is present, enter the complete sequence, and run the check. Select Generate when you have only the body digits; the tool calculates and appends the checksum. Spaces and hyphens are removed before calculation. Use dummy data, not a real account or card number.

What the Luhn algorithm checks

Luhn (modulus 10) is an error-detection checksum used in credit-card numbering, IMEI identifiers, and some account or ID systems. It detects every single-digit substitution and most adjacent transpositions. It is not encryption, authentication, or proof that an identifier was issued.

Exact validation steps

  1. Starting at the rightmost check digit, move left and double every second digit. The check digit is not doubled.
  2. If a doubled value exceeds 9, subtract 9. This equals adding the decimal digits of a product from 10 through 18.
  3. Add all transformed and unchanged digits. The sequence passes when sum mod 10 = 0.

How generation differs

Append a temporary zero to the body, compute the same sum, then use (10 - (sum mod 10)) mod 10. The outer modulo produces zero when the sum is already divisible by ten.

使い方

末尾のチェックディジットを含む番号を調べる場合は「検証モード」、末尾が未確定で正しい1桁を補いたい場合は「生成モード」を選びます。検証では合否・合計・mod 10の剰余を、生成ではチェックディジットと補完後の番号を表示します。実在する番号ではなくダミー値を利用してください。

Luhnアルゴリズムが使われる場面

クレジットカード番号、端末のIMEI番号、一部の会員番号・各種ID体系で、転記や手入力の誤りを見つけるチェックディジットとして採用されています。番号の意味や発行状態を調べる仕組みではなく、各体系の桁数・接頭辞などの規則も別途確認が必要です。

検証の数学的手順

右端のチェックディジットを1桁目として左へ進み、右から2桁おきの数字を2倍します。積が9を超えたら9を引き、未処理の桁と合わせて総和を求めます。総和を10で割った余りが0なら合格です。本体7992739871に候補3を付けると合計70となり、70 mod 10 = 0です。

なぜチェックディジットが必要か

数字1桁の誤入力を全て検出でき、多くの隣接桁入れ替えも検出できます。一方、暗号学的な改ざん検知能力はなく、意図的に規則を満たす番号を作るのは容易です。本人確認やセキュリティ検証には使えません。

よくあるエラーと対処 / Common Errors and Fixes

症状 / Symptom原因 / Cause対処 / Fix
有効なはずなのに不合格
Expected value fails
本体だけを検証した / The final check digit is missingチェックディジットを含む全桁を入力 / Enter the complete sequence
数字以外のエラー
Non-digit error
全角数字・記号・説明文が混在 / Unsupported characters are included半角数字に直す。空白とハイフンは可 / Use ASCII digits
生成結果が1桁長い
Result is one digit longer
既存のチェック桁込みで生成 / A check digit was included末尾を除いてから生成 / Remove the old check digit
合格を実在性と誤認
Pass mistaken for validity
計算上の整合性だけを検査 / Arithmetic consistency only発行・利用可否の確認には使わない / Do not infer issuance or usability

言語別コード例 / Code Examples

JavaScript

function isLuhnValid(value) {
  const digits = value.replace(/[\s-]/g, "");
  if (!/^\d+$/.test(digits)) return false;
  let sum = 0, doubled = false;
  for (let i = digits.length - 1; i >= 0; i--) {
    let n = Number(digits[i]);
    if (doubled && (n *= 2) > 9) n -= 9;
    sum += n; doubled = !doubled;
  }
  return sum % 10 === 0;
}

Python

def luhn_valid(value: str) -> bool:
    digits = [int(c) for c in value if c.isdigit()]
    total = 0
    for index, digit in enumerate(reversed(digits)):
        if index % 2 == 1:
            digit *= 2
            if digit > 9: digit -= 9
        total += digit
    return bool(digits) and total % 10 == 0

Go

func LuhnValid(s string) bool {
    sum, position := 0, 0
    for i := len(s) - 1; i >= 0; i-- {
        if s[i] < '0' || s[i] > '9' { return false }
        n := int(s[i] - '0')
        if position%2 == 1 { n *= 2; if n > 9 { n -= 9 } }
        sum += n; position++
    }
    return len(s) > 0 && sum%10 == 0
}

Before / After 実例

以下はアルゴリズム説明専用のダミー数字列で、実在するカード、口座、端末、利用可能な番号を表しません。

生成 / Generate

Before (本体): 7992739871
Check digit:    3
After (補完後): 79927398713

検証 / Validate

79927398713 → sum 70 → 70 mod 10 = 0 → Valid
79927398714 → sum 71 → 71 mod 10 = 1 → Invalid

Why browser-only? / なぜブラウザ完結か

Identifiers processed by Luhn can include sensitive values such as card-like numbers. Normalization, validation, and generation run entirely inside your browser tab. No API request is made, and input is never sent to or stored on a server. Even so, use invented test data whenever possible.

カード番号のように機密になりうる数字列を扱う可能性があるため、入力の正規化、検証、生成の全てをブラウザ内のJavaScriptだけで実行します。入力値をAPIやサーバーへ送信・保存する処理は一切ありません。それでも実在する番号は入力せず、ダミー数字列を使用してください。

関連ツール・関連ガイド / Related Tools and Guides

開発をもっと効率的に

PR