Skip to content

creditCard

Validates a credit card number using the Luhn checksum algorithm and detects the card network.

Options

OptionTypeDefaultDescription
messagestringlocale defaultCustom error message

Result meta

When valid, the result includes a meta object with the detected card type:

js
// result from validate()
{ valid: true, meta: { type: 'visa' } }
meta.typeNetwork
"visa"Visa
"mastercard"Mastercard
"amex"American Express
"discover"Discover
"dinersclub"Diners Club
"jcb"JCB
"maestro"Maestro
"unionpay"UnionPay
"unknown"Unrecognised prefix

Use meta.type to display a live card logo while the user types.

Playground

Valid values

ValueNotes
"4532015112830366"Visa
"5425233430109903"Mastercard
"378282246310005"American Express

Invalid values

ValueReason
"4532015112830367"Fails Luhn checksum
"1234567890123456"Not a valid card number

Notes

  • Empty string ("") always returns valid: true — validators only run on non-empty values. Combine with notEmpty to require a value.
  • Accepts digits with or without spaces/hyphens.

Released under the MIT License.