TheAlgorithms/JavaScript · error · TypeError
${creditCardString} is an invalid credit card number because
Error message
${creditCardString} is an invalid credit card number because it has nonnumerical characters. What it means
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.
Source
Thrown at String/ValidateCreditCard.js:43
currentDigit += 1
}
}
validationSum += currentDigit
})
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
}View on GitHub (pinned to 5c39e87a9a)
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.
Example fix
// before validateCreditCard(cardField.value) // '4532-0151-1283-0366' // after const digits = cardField.value.replace(/\D/g, '') validateCreditCard(digits)
Defensive patterns
Strategy: validation
Validate before calling
function validateCardNormalized(v) {
if (typeof v !== 'string') throw new TypeError('not a string')
const digits = v.replace(/\D/g, '')
if (!/^\d+$/.test(digits)) throw new TypeError('nonnumerical characters')
return validateCreditCard(digits)
} Type guard
const isAllDigits = (s) => typeof s === 'string' && /^\d+$/.test(s)
Try / catch
try { validateCreditCard(card) } catch (e) { if (/nonnumerical/.test(e.message)) { card = card.replace(/\D/g, ''); /* retry */ } else throw e } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The given value is not a string
- The arg must be a valid, non empty string
- The given value is not a string
- The given value is not a string
- ${creditCardString} is an invalid credit card number because
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/a696e545e6593e7f.
Report an issue: GitHub.