creditcardutils
A general purpose javascript library for credit card number validation and formatting. Based on jondavidjohn/payform
and jquery.payment
. Usable in React Native and Node.
Supported card types:
- Visa
- MasterCard
- American Express
- Diners Club
- Discover
- UnionPay
- JCB
- Visa Electron
- Maestro
- Forbrugsforeningen
- Dankort
(Custom card types are also supported)
Installation / Usage
npm (Node and Browserify)
npm install creditcardutils --save
var creditcardutils = require('creditcardutils');
creditcardutils.formatCardNumber(input);
creditcardutils.validateCardNumber('4242 4242 4242 4242');
creditcardutils.parseCardType('4242 4242 4242 4242');
API
General Formatting and Validation
creditcardutils.formatCardNumber(input)
Formats card numbers:
- Includes a space between every 4 digits
- Restricts input to numbers
- Limits to 16 numbers
- Supports American Express formatting
Example:
creditcardutils.formatCardNumber(input);
creditcardutils.validateCardNumber(number)
Validates a card number:
- Validates numbers
- Validates Luhn algorithm
- Validates length
Example:
creditcardutils.validateCardNumber('4242 4242 4242 4242');
creditcardutils.validateCardExpiry(month, year)
Validates a card expiry:
- Validates numbers
- Validates in the future
- Supports year shorthand
Example:
creditcardutils.validateCardExpiry('05', '20');
creditcardutils.validateCardExpiry('05', '2015');
creditcardutils.validateCardExpiry('05', '05');
creditcardutils.validateCardCVC(cvc, type)
Validates a card CVC:
- Validates number
- Validates length to 4
Example:
creditcardutils.validateCardCVC('123');
creditcardutils.validateCardCVC('123', 'amex');
creditcardutils.validateCardCVC('1234', 'amex');
creditcardutils.validateCardCVC('12344');
creditcardutils.parseCardType(number)
Returns a card type. Either:
visa
mastercard
amex
dinersclub
discover
unionpay
jcb
visaelectron
maestro
forbrugsforeningen
dankort
The function will return null
if the card type can't be determined.
Example:
creditcardutils.parseCardType('4242 4242 4242 4242');
creditcardutils.parseCardType('hello world?');
creditcardutils.parseCardExpiry(string)
Parses a credit card expiry in the form of MM/YYYY, returning an object containing the month
and year
. Shorthand years, such as 13
are also supported (and converted into the longhand, e.g. 2013
).
creditcardutils.parseCardExpiry('03 / 2025');
creditcardutils.parseCardExpiry('05 / 04');
This function doesn't perform any validation of the month or year; use creditcardutils.validateCardExpiry(month, year)
for that.
Custom Cards
creditcardutils.cards
Array of objects that describe valid card types. Each object should contain the following fields:
{
type: 'mastercard',
pattern: /^5[0-5]/,
length: [16],
cvcLength: [3],
luhn: true,
format: /(\d{1,4})/g
}
When identifying a card type, the array is traversed in order until the card number matches a pattern
. For this reason, patterns with higher specificity should appear towards the beginning of the array.
Development
Please see CONTRIBUTING.md.