gchq/CyberChef · error · Error

Character: ${elem} is not valid in radix ${radix}.

Error message

Character: ${elem} is not valid in radix ${radix}.

What it means

Thrown by the Luhn Checksum operation's internal checksum() helper when a character in the input cannot be parsed as a digit in the configured radix. It calls parseInt(elem, radix) per character; if that yields NaN the helper throws a plain Error (not OperationError) at LuhnChecksum.mjs:51. Because the radix also constrains which characters are legal, a radix-10 run rejects any letter, and a radix-16 run rejects characters outside 0-9A-F.

Source

Thrown at src/core/operations/LuhnChecksum.mjs:51

            }
        ];
    }

    /**
     * Generates the Luhn checksum from the input.
     *
     * @param {string} inputStr
     * @returns {number}
     */
    checksum(inputStr, radix = 10) {
        let even = false;
        return inputStr.split("").reverse().reduce((acc, elem) => {
            // Convert element to an integer based on the provided radix.
            let temp = parseInt(elem, radix);

            // If element is not a valid number in the given radix.
            if (isNaN(temp)) {
                throw new Error("Character: " + elem + " is not valid in radix " + radix + ".");
            }

            // If element is in an even position
            if (even) {
                // Double the element and sum the quotient and remainder.
                temp = 2 * temp;
                temp = Math.floor(temp / radix) + (temp % radix);
            }

            even = !even;
            return acc + temp;
        }, 0) % radix; // Use radix as the modulus base
    }

    /**
     * @param {string} input
     * @param {Object[]} args
     * @returns {string}

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Strip all non-digit characters for the chosen radix before running (e.g. remove spaces, dashes, letters when radix=10).
  2. If you need letters in the checksum, raise the radix to an even value that covers your alphabet (e.g. 36 for 0-9A-Z) and ensure all input chars are valid in that base.
  3. Pre-validate each character with parseInt(c, radix) and report which character is illegal.
  4. Note this is a plain Error, so wrap the call in try/catch if calling checksum()/run() programmatically.

Example fix

// before — letters passed with radix 10
luhn.run('7992739871A', [10]);

// after — strip invalid chars first
const clean = '79927398713'.replace(/[^0-9]/g, '');
luhn.run(clean, [10]);
Defensive patterns

Strategy: try-catch

Validate before calling

function cleanForLuhn(str, radix) {
  const re = new RegExp(`[^0-9a-zA-Z]`, 'g');
  let s = str.replace(re, '');
  // ensure every remaining char is valid in this radix
  for (const c of s.toUpperCase()) {
    if (isNaN(parseInt(c, radix))) {
      throw new Error(`Character ${c} invalid for radix ${radix}`);
    }
  }
  return s;
}
luhn.run(cleanForLuhn(input, radix), [radix]);

Type guard

function isRadixValidString(v: unknown, radix: number): v is string {
  if (typeof v !== 'string') return false;
  return [...v].every(c => !isNaN(parseInt(c, radix)));
}

Try / catch

try {
  luhn.run(input, [radix]);
} catch (e) {
  // checksum() throws a plain Error, not OperationError
  if (e instanceof Error && /is not valid in radix/.test(e.message)) {
    // handle the bad-character case (e.g. report and clean input)
  } else throw e;
}

Prevention

When it happens

Trigger: Running 'Luhn Checksum' with input containing a character invalid for the radix — e.g. radix=10 with the letter 'A', radix=2 with '2', or any whitespace/punctuation. The error surfaces from checksum() which is called by run() at LuhnChecksum.mjs:84-85.

Common situations: Leaving whitespace or a delimiter in the input string; mixing alphabets (letters passed when radix=10); copying an identifier that includes separators/dashes; forgetting that Luhn mod-N requires all chars to be valid digits in the chosen base.

Related errors


AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13). Data as JSON: /api/errors/2433827d5d12a949. Report an issue: GitHub.