Create An Interactive Credit Card Form In jQuery - Card.js

Create An Interactive Credit Card Form In jQuery - Card.js
File Size: 70 KB
Views Total:
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

The jQuery Card.js plugin makes it easier to create an interactive credit card form for the payout page of your e-commerce or shopping websites.

Features:

  • Card type detection.
  • Field validation.
  • Input masking.
  • Custom placeholders for number, name, expiry and CVC.
  • Also can be implemented in vanilla JavaScript.

Installation:

# NPM
$ npm install card

# Bower
$ bower install card

How to use it:

1. Load the core stylesheet card.css in the head section of the webpage.

<link href="card.css" rel="stylesheet">

2. Load the JavaScript file jquery.card.js(For jQuery) or card.js(For Vanilla JavaScript) at the bottom of the webpage

<script src="//code.jquery.com/jquery.min.js"></script>
<script src="dist/jquery.card.js"></script>
<!-- OR -->
<script src="dist/card.js"></script>

3. Create the card wrapper and number/name/expiry/CVC input fields on the webpage.

<div class="card-wrapper"></div>

<div class="form-container active">
  <form action="">
    <input placeholder="Card number" type="tel" name="number">
    <input placeholder="Full name" type="text" name="name">
    <input placeholder="MM/YY" type="tel" name="expiry">
    <input placeholder="CVC" type="number" name="cvc">
  </form>
</div>

4. Initialize the Card.js and we're done.

// jQuery
$('form').card({
  ontainer: '.card-wrapper'
});

// Vanilla JavaScript
new Card({
  form: document.querySelector('form'),
  container: '.card-wrapper'
});

5. All possible options to customize the Credit Card Form.

$('form').card({

  // number formatting
  formatting: true,

  // selectors
  formSelectors: {
    numberInput: 'input[name="number"]',
    expiryInput: 'input[name="expiry"]',
    cvcInput: 'input[name="cvc"]',
    nameInput: 'input[name="name"]'
  },
  cardSelectors: {
    cardContainer: '.jp-card-container',
    card: '.jp-card',
    numberDisplay: '.jp-card-number',
    expiryDisplay: '.jp-card-expiry',
    cvcDisplay: '.jp-card-cvc',
    nameDisplay: '.jp-card-name'
  },

  // custom messages
  messages: {
    validDate: 'valid\nthru',
    monthYear: 'month/year'
  },

  // custom placeholders
  placeholders: {
    number: '&bull;&bull;&bull;&bull; &bull;&bull;&bull;&bull; &bull;&bull;&bull;&bull; &bull;&bull;&bull;&bull;',
    cvc: '&bull;&bull;&bull;',
    expiry: '&bull;&bull;/&bull;&bull;',
    name: 'Full Name'
  },

  // enable input masking 
  masks: {
    cardNumber: false
  },

  // valid/invalid CSS classes
  classes: {
    valid: 'jp-card-valid',
    invalid: 'jp-card-invalid'
  },

  // debug mode
  debug: false
  
});

Change log:

2017-08-21

  • v2.3.0

This awesome jQuery plugin is developed by jessepollak. For more Advanced Usages, please check the demo page or visit the official website.