TheAlgorithms/JavaScript · error · Error
${creditCardString} is an invalid credit card number because
Error message
${creditCardString} is an invalid credit card number because of its length. What it means
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.
Source
Thrown at String/ValidateCreditCard.js:47
})
return validationSum % 10 === 0
}
const validateCreditCard = (creditCardString) => {
const validStartSubString = ['4', '5', '6', '37', '34', '35'] // Valid credit card numbers start with these numbers
if (typeof creditCardString !== 'string') {
throw new TypeError('The given value is not a string')
}
const errorMessage = `${creditCardString} is an invalid credit card number because `
if (isNaN(creditCardString)) {
throw new TypeError(errorMessage + 'it has nonnumerical characters.')
}
const creditCardStringLength = creditCardString.length
if (!(creditCardStringLength >= 13 && creditCardStringLength <= 16)) {
throw new Error(errorMessage + 'of its length.')
}
if (
!validStartSubString.some((subString) =>
creditCardString.startsWith(subString)
)
) {
throw new Error(errorMessage + 'of its first two digits.')
}
if (!luhnValidation(creditCardString)) {
throw new Error(errorMessage + 'it fails the Luhn check.')
}
return true
}
export { validateCreditCard }
View on GitHub (pinned to 5c39e87a9a)
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.
Example fix
// before
validateCreditCard(raw) // length mismatch after a buggy sanitizer
// after
const digits = raw.replace(/[\s-]/g, '')
if (digits.length >= 13 && digits.length <= 16) {
validateCreditCard(digits)
} else {
// surface a user-facing length error
} Defensive patterns
Strategy: validation
Validate before calling
function validateCardLength(v) {
const digits = String(v).replace(/\D/g, '')
if (digits.length < 13 || digits.length > 16) {
throw new Error('card number must be 13-16 digits')
}
return validateCreditCard(digits)
} Type guard
const hasCardLength = (s) => typeof s === 'string' && s.length >= 13 && s.length <= 16
Try / catch
try { validateCreditCard(card) } catch (e) { if (/of its length/.test(e.message)) { /* prompt user to re-enter */ } else throw e } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The given value is not a string
- ${creditCardString} is an invalid credit card number because
- ${creditCardString} is an invalid credit card number because
- ${creditCardString} is an invalid credit card number because
- Invalid day value.
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/a8efe42babf5ecbc.
Report an issue: GitHub.