Utility methods for formatting and validating credit cards. With a minimal footprint and a flexible API, it's suitable for both Node and the browser.
# npm
$ npm install creditcards
# bower
$ bower install creditcards
- Arguments:
card
(object)number
(string)expirationMonth
(number)expirationYear
(number)cvc
(string)
- Returns:
- object
card
type
(string) - the type of the provided cardnumber
(string)expirationMonth
(number)expirationYear
(number)cvc
(string)
validCardNumber
(boolean)validExpirationMonth
(boolean)validExpirationYear
(boolean)validCvc
(boolean)expired
(boolean) - whether the expiration date has passed
- object
Removes all non-numeric characters from a card number, including punctuation and spacing. If a non-string is provided, it returns an empty string.
Returns the matched card type, or undefined
if there was no match. Valid card types are:
- Visa
- MasterCard
- American Express
- Diners Club
- Discover
- JCB
Checks the card number's validity using the Luhn algorithm.
Validates the number using card.luhn
and checks that it matches any type
(or a specific type
if provided).
Checks whether a card verification code is a valid 3-4 digit numeric string. If a type
is provided, the length will be validated for the card type (4 for American Express, 3 for others).
Checks whether a given month and year pair (both Number
) are in the past.
Casts the provided month
value to a Number
. All of the following will be 5
after parsing:
5
'05'
'5'
Returns undefined
for non-numeric values.
Checks whether the provided month (Number
) is valid (between 1 and 12).
Casts the provided year value to a Number
. If pad
is true
, year
is assumed to be a two digit number or numeric string. All of the following are equivalent:
expiration.year.parse(2014)
expiration.year.parse('2014')
expiration.year.parse('14', true)
expiration.year.parse(14, true)
Returns undefined
for non-numeric values.
Casts the provided year (Number
) to a String
. If strip
is true
, year
is assumed to be a four digit number and will be converted to a two digit number.
expiration.year.format(2014) === '2014'
expiration.year.format(2014, true) === '14'
Checks whether the provided year (Number
) is valid (> 0).
Checks whether a given year (Number
) is in the past.
There are lots of other useful credit card validation and parsing modules. creditcards specifically takes inspiration from credit-card, but there are many others, including the popular jQuery.payment. creditcards was specifically designed for browser use for handling payment forms. That means:
- Browserified, it's only a few kilobytes, even before minification.
- It provides an API for parsing user inputs.
- It has no DOM-related code. You can use it in Node and in the browser you get full control over how your card inputs are handled.
- The card type is optional.
- angular-credit-cards: A set of Angular directives for building credit card forms powered by creditcards