{"record":{"id":"7f33eff07f4d595d","repo":"TheAlgorithms/JavaScript","slug":"creditcardstring-is-an-invalid-credit-card-numb-7f33ef","errorCode":null,"errorMessage":"${creditCardString} is an invalid credit card number because of its first two digits.","messagePattern":"(.+?) is an invalid credit card number because of its first two digits\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"String/ValidateCreditCard.js","lineNumber":54,"sourceCode":"\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":36,"sourceCodeEnd":64,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/ValidateCreditCard.js#L36-L64","documentation":"Thrown by validateCreditCard() when the string passes type, NaN, and length checks but does not start with one of the recognized Issuer Identification Number prefixes: '4' (Visa), '5' (Mastercard), '6' (Discover), '37','34','35' (Amex). A generic Error signalling the issuer could not be matched.","triggerScenarios":"Calling validateCreditCard with a string starting with '3' but not '34','35','37' (e.g. '3012345678901234' — Diners/JCB not in the list); starting with '1' or '2'; starting with '6011' (Discover uses '6' prefix so this passes); a valid-Luhn synthetic test number that uses an unrecognized prefix.","commonSituations":"Test fixtures using Luhn-valid numbers from issuers not on the list (e.g. UnionPay '62', JCB '35' — note 35 IS in the list); card networks added since the function was written; typos in the first digits; using a generated test card the library does not recognize.","solutions":["Check the prefix against the recognized list ['4','5','6','37','34','35'] and either accept the rejection or extend validStartSubString in your own fork.","For tests, use a number whose prefix is on the list (e.g. Visa-style '4...').","If you must support other issuers (UnionPay, JCB '35' already supported, etc.), wrap or fork the function and extend validStartSubString.","Verify you are not double-trimming digits from the start during sanitization."],"exampleFix":"// before\nvalidateCreditCard('6221260000000000') // UnionPay '62' not recognized\n\n// after (if you control the function)\nconst validStartSubString = ['4', '5', '6', '37', '34', '35', '62']\n// or: use a test card with a recognized prefix\nvalidateCreditCard('4111111111111111')","handlingStrategy":"validation","validationCode":"const KNOWN_PREFIXES = ['4','5','6','37','34','35']\nfunction hasKnownIssuer(s) {\n  return KNOWN_PREFIXES.some((p) => s.startsWith(p))\n}\n// call hasKnownIssuer(digits) before validateCreditCard","typeGuard":"const hasKnownIssuerPrefix = (s) => ['4','5','6','37','34','35'].some((p) => typeof s === 'string' && s.startsWith(p))","tryCatchPattern":"try { validateCreditCard(card) } catch (e) { if (/first two digits/.test(e.message)) { /* unsupported or unknown issuer */ } else throw e }","preventionTips":["Use test cards from a recognized issuer (Visa '4...', Mastercard '5...').","If supporting other issuers, fork and extend validStartSubString explicitly.","Document which issuers your integration recognizes for your users."],"tags":["validation","credit-card","business-rule","issuer-detection"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}