Credit card validate
This library is for testing the validity of credit cards. It can can check number of digits, luhn algorithm, number of digits in code (CVV, CVC, ...), expiration or type of Credit card.
There are 7 card types built in - American Express, Diners Club, Discover, JCB, Maestro, MasterCard and Visa. I can't guarantee that card type validation will work on 100%. There is not some trustworthy source with information how to detect it right. Most of information comes from https://en.wikipedia.org/wiki/Payment_card_number. You can also add your own credit card types.
You can use package as npm module. Just install it:
npm install credit-card-validate --save-dev
and use it in you project:
var validators = ;var card = '4111111111111111' '2017-29' '122';console;
You can also install it as bower package:
bower install credit-card-validate
and use is very similar as in npm
var card = '5451418521738499' '2018-29' '432';console;
It can be also used with RequireJS, CommonJS, Browserify or Webpack.
Predefined card types
Following card types are currently build in:
- American Express
- Diners Club
- Discover
- JCB
- Maestro
- MasterCard
- Visa
You can get various information from classes from static methods:
// will provide list of length card could be// eg [13, 16, 19]CardValidatecardsVisa // get length of code (CVV, CVC, ...)// eg 3CardValidatecardsVisa // where can be code found? back or front// eg frontCardValidatecardsVisa
You can also create object and check validity of various property or all of them by once. To create a card object just push to constructor card number, expire date and code.
var card = CardValidatecards; card;card; // check that card number is in range on card typecard; card; card; // check all tests at onecard;
Custom card type
You can create your own custom credit card. In example I'm using ECMAScript 2015 syntax.
CreditCard { supernumber expire code; thisname = 'You Own Card'; thisallowedDigits = YouOwnCard; thisallowedCodeDigits = YouOwnCard; thiscodeLocation = YouOwnCard; thisstartDigits = YouOwnCard; } static { // card have to start with 98 or 99 return '99' '98'; } static : string return 'back'; static : number return 3; static : Array<number> return 13 16 19;
Detect credit card type
With last feature you can detect credit card type based on number. You can to create object of CreditCardFactory a fill it with all credit card you want to support:
var creditCardFactory = CardValidate; // this will return array with both DinersClub and JCB, because number number can be bothconsole