Validate Credit Card using jQuery Plugin

Have you ever come across a situation where you need to validate the credit card? Well, there is a good news now. There is a new plugin jQuery Credit Card Validator created by Pawel Decowski, which 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.

How to use it?

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

How it works?

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 visa
  • pattern — 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 otherwise
  • luhn_valid — true if the Luhn checksum is correct, false otherwise

Supported Cards

Below is the list of supported cards.

  • American Express — amex
  • Diners Club Carte Blanche — diners_club_carte_blanche
  • Diners Club International — diners_club_international
  • Diners Club United States & Canada — diners_club_us_and_ca
  • Discover Card — discover
  • JCB — jcb
  • Laser — laser
  • Maestro — maestro
  • MasterCard — mastercard
  • Visa — visa
  • Visa Electron — visa_electron

Feel free to contact me for any help related to jQuery, I will gladly help you.



Responsive Menu
Add more content here...