{"record":{"id":"a696e545e6593e7f","repo":"TheAlgorithms/JavaScript","slug":"creditcardstring-is-an-invalid-credit-card-numb","errorCode":null,"errorMessage":"${creditCardString} is an invalid credit card number because it has nonnumerical characters.","messagePattern":"(.+?) is an invalid credit card number because it has nonnumerical characters\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"String/ValidateCreditCard.js","lineNumber":43,"sourceCode":"        currentDigit += 1\n      }\n    }\n    validationSum += currentDigit\n  })\n\n  return validationSum % 10 === 0\n}\n\nconst validateCreditCard = (creditCardString) => {\n  const validStartSubString = ['4', '5', '6', '37', '34', '35'] // Valid credit card numbers start with these numbers\n\n  if (typeof creditCardString !== 'string') {\n    throw new TypeError('The given value is not a string')\n  }\n\n  const errorMessage = `${creditCardString} is an invalid credit card number because `\n  if (isNaN(creditCardString)) {\n    throw new TypeError(errorMessage + 'it has nonnumerical characters.')\n  }\n  const creditCardStringLength = creditCardString.length\n  if (!(creditCardStringLength >= 13 && creditCardStringLength <= 16)) {\n    throw new Error(errorMessage + 'of its length.')\n  }\n  if (\n    !validStartSubString.some((subString) =>\n      creditCardString.startsWith(subString)\n    )\n  ) {\n    throw new Error(errorMessage + 'of its first two digits.')\n  }\n  if (!luhnValidation(creditCardString)) {\n    throw new Error(errorMessage + 'it fails the Luhn check.')\n  }\n\n  return true\n}","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/ValidateCreditCard.js#L25-L61","documentation":"Thrown by validateCreditCard() when the string passes the type check but isNaN(creditCardString) is true — i.e. the string contains characters that are not parseable as a number (spaces, dashes, letters). A TypeError, because the function expects a pure-digit string. The interpolated value is echoed into the message.","triggerScenarios":"Calling validateCreditCard('4532-0151-1283-0366') (dashes), validateCreditCard('4532 0151 1283 0366') (spaces), validateCreditCard('4532abcd12830366'), validateCreditCard(''), validateCreditCard('not-a-card'). An empty string isNaN('') === false, so '' will NOT throw here — but a whitespace-only or alphabetic string will.","commonSituations":"Users typing card numbers with spaces/dashes; formatters that group digits with separators; copy-paste from a formatted source; forgetting to strip whitespace from raw input before validation.","solutions":["Strip non-digit characters before validating: validateCreditCard(raw.replace(/\\D/g, '')).","Normalize input: remove spaces and dashes, e.g. raw.replace(/[\\s-]/g, '').","Validate digit-only upstream with /^\\d+$/.test(normalized) before calling.","Reject obviously malformed input at the form layer with an inputmode='numeric' field."],"exampleFix":"// before\nvalidateCreditCard(cardField.value) // '4532-0151-1283-0366'\n\n// after\nconst digits = cardField.value.replace(/\\D/g, '')\nvalidateCreditCard(digits)","handlingStrategy":"validation","validationCode":"function validateCardNormalized(v) {\n  if (typeof v !== 'string') throw new TypeError('not a string')\n  const digits = v.replace(/\\D/g, '')\n  if (!/^\\d+$/.test(digits)) throw new TypeError('nonnumerical characters')\n  return validateCreditCard(digits)\n}","typeGuard":"const isAllDigits = (s) => typeof s === 'string' && /^\\d+$/.test(s)","tryCatchPattern":"try { validateCreditCard(card) } catch (e) { if (/nonnumerical/.test(e.message)) { card = card.replace(/\\D/g, ''); /* retry */ } else throw e }","preventionTips":["Strip separators (spaces, dashes) with .replace(/\\D/g, '') before validating.","Set inputmode='numeric' on card fields to discourage alphabetic input.","Validate digit-only shape upstream and surface a friendly message."],"tags":["string","validation","credit-card","sanitization","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}