{"record":{"id":"a8efe42babf5ecbc","repo":"TheAlgorithms/JavaScript","slug":"creditcardstring-is-an-invalid-credit-card-numb-a8efe4","errorCode":null,"errorMessage":"${creditCardString} is an invalid credit card number because of its length.","messagePattern":"(.+?) is an invalid credit card number because of its length\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"String/ValidateCreditCard.js","lineNumber":47,"sourceCode":"  })\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}\n\nexport { validateCreditCard }\n","sourceCodeStart":29,"sourceCodeEnd":64,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/ValidateCreditCard.js#L29-L64","documentation":"Thrown by validateCreditCard() after the type and NaN checks pass, when the string's length is not between 13 and 16 inclusive. This is a generic Error (not TypeError) because the value is well-formed data that fails a business rule. Most card networks issue numbers in this length range.","triggerScenarios":"Calling validateCreditCard('4532015112830') (12 digits, too short), validateCreditCard('4532015112830366789') (19 digits, too long), validateCreditCard('4') (1 digit), or any digit-only string whose length falls outside [13,16]. A stripped input that lost digits during sanitization commonly lands here.","commonSituations":"User mistyped or dropped digits; aggressive sanitizer stripped legitimate digits (e.g. removed 0s); a non-card number (e.g. CVV, ZIP) accidentally routed into validation; concatenating card fields incorrectly.","solutions":["Confirm the expected length range (13–16 digits) for the card network you target.","Re-check that your sanitizer removed only separators, not digits: raw.replace(/[\\s-]/g, '') (not /\\D/g if you have legitimate concerns — though digits are what you want).","Show a length hint to the user in the form ('Card number must be 13–16 digits').","Log the sanitized length during debugging to see what reached the validator."],"exampleFix":"// before\nvalidateCreditCard(raw) // length mismatch after a buggy sanitizer\n\n// after\nconst digits = raw.replace(/[\\s-]/g, '')\nif (digits.length >= 13 && digits.length <= 16) {\n  validateCreditCard(digits)\n} else {\n  // surface a user-facing length error\n}","handlingStrategy":"validation","validationCode":"function validateCardLength(v) {\n  const digits = String(v).replace(/\\D/g, '')\n  if (digits.length < 13 || digits.length > 16) {\n    throw new Error('card number must be 13-16 digits')\n  }\n  return validateCreditCard(digits)\n}","typeGuard":"const hasCardLength = (s) => typeof s === 'string' && s.length >= 13 && s.length <= 16","tryCatchPattern":"try { validateCreditCard(card) } catch (e) { if (/of its length/.test(e.message)) { /* prompt user to re-enter */ } else throw e }","preventionTips":["Show a 13–16 digit hint in the form UI.","Audit sanitizers so they remove only separators, never digits.","Confirm you are not feeding CVV/ZIP into the card validator."],"tags":["validation","credit-card","business-rule","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}