{"record":{"id":"2433827d5d12a949","repo":"gchq/CyberChef","slug":"character-elem-is-not-valid-in-radix-radix","errorCode":null,"errorMessage":"Character: ${elem} is not valid in radix ${radix}.","messagePattern":"Character: (.+?) is not valid in radix (.+?)\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/core/operations/LuhnChecksum.mjs","lineNumber":51,"sourceCode":"            }\n        ];\n    }\n\n    /**\n     * Generates the Luhn checksum from the input.\n     *\n     * @param {string} inputStr\n     * @returns {number}\n     */\n    checksum(inputStr, radix = 10) {\n        let even = false;\n        return inputStr.split(\"\").reverse().reduce((acc, elem) => {\n            // Convert element to an integer based on the provided radix.\n            let temp = parseInt(elem, radix);\n\n            // If element is not a valid number in the given radix.\n            if (isNaN(temp)) {\n                throw new Error(\"Character: \" + elem + \" is not valid in radix \" + radix + \".\");\n            }\n\n            // If element is in an even position\n            if (even) {\n                // Double the element and sum the quotient and remainder.\n                temp = 2 * temp;\n                temp = Math.floor(temp / radix) + (temp % radix);\n            }\n\n            even = !even;\n            return acc + temp;\n        }, 0) % radix; // Use radix as the modulus base\n    }\n\n    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {string}","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/LuhnChecksum.mjs#L33-L69","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Strip all non-digit characters for the chosen radix before running (e.g. remove spaces, dashes, letters when radix=10).","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.","Pre-validate each character with parseInt(c, radix) and report which character is illegal.","Note this is a plain Error, so wrap the call in try/catch if calling checksum()/run() programmatically."],"exampleFix":"// before — letters passed with radix 10\nluhn.run('7992739871A', [10]);\n\n// after — strip invalid chars first\nconst clean = '79927398713'.replace(/[^0-9]/g, '');\nluhn.run(clean, [10]);","handlingStrategy":"try-catch","validationCode":"function cleanForLuhn(str, radix) {\n  const re = new RegExp(`[^0-9a-zA-Z]`, 'g');\n  let s = str.replace(re, '');\n  // ensure every remaining char is valid in this radix\n  for (const c of s.toUpperCase()) {\n    if (isNaN(parseInt(c, radix))) {\n      throw new Error(`Character ${c} invalid for radix ${radix}`);\n    }\n  }\n  return s;\n}\nluhn.run(cleanForLuhn(input, radix), [radix]);","typeGuard":"function isRadixValidString(v: unknown, radix: number): v is string {\n  if (typeof v !== 'string') return false;\n  return [...v].every(c => !isNaN(parseInt(c, radix)));\n}","tryCatchPattern":"try {\n  luhn.run(input, [radix]);\n} catch (e) {\n  // checksum() throws a plain Error, not OperationError\n  if (e instanceof Error && /is not valid in radix/.test(e.message)) {\n    // handle the bad-character case (e.g. report and clean input)\n  } else throw e;\n}","preventionTips":["Strip delimiters/spaces from the input before running Luhn.","Match your alphabet to an even radix that covers every input character.","Remember checksum() throws a plain Error — catch accordingly in code."],"tags":["luhn","checksum","validation","radix","number-format"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}