Brand new validator is coming. Subscribe to find out first.

Major brands

It recognises all major card brands: Visa, Mastercard, Maestro, American Express, Diners Club — to name a few. And we’re always looking to add more!

Free forever

jQuery Credit Card Validator is free and we promise you that it’ll stay free. What’s more, we’ll give you free support. Just tweet us or create a new issue on GitHub if you find a problem.

Open source

jQuery Credit Card Validator is MIT-licensed. That means you can do whatever you want with it — use it in commercial projects, modify it, sell it — as long as you include the copyright notice and licence text.

How it works

jQuery Credit Card Validator is written in CoffeeScript and (surprise, surprise!) is a jQuery plugin. It analyses the card number in the input field on which it was called, and returns an object with four properties:

  • card_type — an object with the below properties, or null if card type unknown
    • name — one of the strings describing the card type, eg visa
    • range — range string used to match the card type, eg '6011, 622126-622925, 644-649, 65'
    • length — a list of valid lengths for the card type, eg [13, 16]
  • validtrue if the number is valid, false otherwise
  • length_validtrue if the number length is valid, false otherwise
  • luhn_validtrue if the Luhn checksum is correct, false otherwise

Installation

NPM

$ npm install jquery-creditcardvalidator --save

Manual

Download the JS file.

How to use it

The function’s signature is:

.validateCreditCard( [ callback ] [, options ] )

Callback

If the callback parameter is specified, the plugin will call it every time the field’s value changes and pass it the result object:

$('#cc_number').validateCreditCard(function(result)
    {
        alert('CC type: ' + result.card_type.name
          + '\nLength validation: ' + result.length_valid
          + '\nLuhn validation: ' + result.luhn_valid);
    });

The value of this inside the callback is set to the input element to which the validator was attached:

$('#cc_number').validateCreditCard(function(result)
    {
        alert(this.val());
    });

The above code will alert the value of the #cc_number field each time it changes.

If the callback parameter is omitted, the function will return the result object:

var result = $('#cc_number').validateCreditCard();

alert('CC type: ' + result.card_type.name
  + '\nLength validation: ' + result.length_valid
  + '\nLuhn validation: ' + result.luhn_valid);

Options

  • accept (optional) — list of accepted credit cards, eg ['visa', 'amex']; if not specified, all supported cards are accepted
    var result = $('#cc_number').validateCreditCard({ accept: ['visa', 'mastercard'] })

Supported cards

Below is a list of supported card types and their string descriptors (which can be accessed via result.card_type.name).

  • American Express — amex
  • Dankort — dankort
  • Diners Club Carte Blanche — diners_club_carte_blanche
  • Diners Club International — diners_club_international
  • Diners Club United States & Canada — mastercard (it is actually a MasterCard)
  • Discover Card — discover
  • JCB — jcb
  • Laser — laser
  • Maestro — maestro
  • MasterCard — mastercard
  • UATP — uatp
  • Visa — visa
  • Visa Electron — visa_electron