jQuery Credit Card Validator detects and validates credit card numbers. It’ll tell you the detected credit card type and whether the number length and Luhn checksum are valid for the type of card.
Try some of these numbers:
The function’s signature is:
.validateCreditCard( callback(result) [, options] )
So the basic usage looks like this:
$('#cc_number').validateCreditCard(function(result)
{
alert('CC type: ' + result.card_type.name
+ '\nLength validation: ' + result.length_valid
+ '\nLuhn validation: + result.luhn_valid');
});
Or with options specified:
$('#cc_number').validateCreditCard(function(result) { ... }, { accept: ['visa', 'mastercard'] })
jQuery Credit Card Validator is written in CoffeeScript
and (surprise, surprise!) is a jQuery plugin. It attaches to the
input event (with a fallback
to the keyup event) and, every time
the number in the input field changes, it calls a function defined by you. It passes a single
parameter to the function — an object with 3 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 visapattern — regular expression used to match the card type, eg /^4/length — an array of valid lengths for the card type, eg [13, 16]
length_valid — true if the number length is valid, false otherwiseluhn_valid — true if the Luhn checksum is correct, false otherwiseThe second parameter currently supports one option:
accept (optional) — list of accepted credit cards, eg ['visa', 'amex']; if not specified, all supported cards are acceptedamexdiners_club_carte_blanchediners_club_internationalmastercard (it is actually a MasterCard)discoverjcblasermaestromastercardvisavisa_electron