:credit_card: make your credit card form better in one line of code

Overview

npm

Card - check out the demo

A better credit card form in one line of code

Card will take any credit card form and make it the best part of the checkout process (without you changing anything). Everything is created with pure CSS, HTML, and Javascript — no images required.

card

Usage (without jQuery)

To use, you'll need to include the Card JavaScript files into your HTML, no CSS link is necessary as the JavaScript file does this for you. You can find the necessary file at /dist/card.js and include it in your HTML like so.

<!-- at the end of BODY -->
<!-- CSS is included via this JavaScript file -->
<script src="/path/to/card.js"></script>

Once you've included those files, you can initialize Card.

var card = new Card({
    // a selector or DOM element for the form where users will
    // be entering their information
    form: 'form', // *required*
    // a selector or DOM element for the container
    // where you want the card to appear
    container: '.card-wrapper', // *required*

    formSelectors: {
        numberInput: 'input#number', // optional — default input[name="number"]
        expiryInput: 'input#expiry', // optional — default input[name="expiry"]
        cvcInput: 'input#cvc', // optional — default input[name="cvc"]
        nameInput: 'input#name' // optional - defaults input[name="name"]
    },

    width: 200, // optional — default 350px
    formatting: true, // optional - default true

    // Strings for translation - optional
    messages: {
        validDate: 'valid\ndate', // optional - default 'valid\nthru'
        monthYear: 'mm/yyyy', // optional - default 'month/year'
    },

    // Default placeholders for rendered fields - optional
    placeholders: {
        number: '•••• •••• •••• ••••',
        name: 'Full Name',
        expiry: '••/••',
        cvc: '•••'
    },

    masks: {
        cardNumber: '•' // optional - mask card number
    },

    // if true, will log helpful messages for setting up Card
    debug: false // optional - default false
});

Installing card from bower

If you're using bower, you can install card.js with:

bower install card --save

Installing card from npm

If you're using npm, you can install card.js with:

npm install --save card

var $ = require("jquery");
// The current card.js code does not explictly require jQuery, but instead uses the global, so this line is needed.
window.jQuery = $;
var card = require("card");

Using multiple inputs for one field

Card can be used in forms where you have multiple inputs that render to a single field (i.e. you have a first and last name input). To use Card with this functionality, just pass in a selector that selects the fields in the correct order. For example,

<html>
<body>
<div class='card-wrapper'></div>
<!-- CSS is included via this JavaScript file -->
<script src="/path/to/card.js"></script>
<form>
    <input type="text" name="number">
    <input type="text" name="first-name"/>
    <input type="text" name="last-name"/>
    <input type="text" name="expiry"/>
    <input type="text" name="cvc"/>
</form>
<script>
var card = new Card({
    form: 'form',
    container: '.card-wrapper',

    formSelectors: {
        nameInput: 'input[name="first-name"], input[name="last-name"]'
    }
});
</script>
</body>
</html>

Rendering with different initial card placeholders

Card renders with default placeholders for card name, number, expiry, and cvc. To override these placeholders, you can pass in a placeholders object.

<html>
<body>
<div class='card-wrapper'></div>
<!-- CSS is included via this JavaScript file -->
<script src="/path/to/card.js"></script>
<form>
    <input type="text" name="number">
    <input type="text" name="name"/>
    <input type="text" name="expiry"/>
    <input type="text" name="cvc"/>
</form>
<script>

var card = new Card({
    form: 'form',
    container: '.card-wrapper',

    placeholders: {
        number: '**** **** **** ****',
        name: 'Arya Stark',
        expiry: '**/****',
        cvc: '***'
    }
});
</script>
</body>
</html>

Translation

To render the card with the strings in a different language, you can pass in a messages object.

<html>
<body>
<div class='card-wrapper'></div>
<!-- CSS is included via this JavaScript file -->
<script src="/path/to/card.js"></script>
<form>
    <input type="text" name="number">
    <input type="text" name="name"/>
    <input type="text" name="expiry"/>
    <input type="text" name="cvc"/>
</form>
<script>

var card = new Card({
    form: 'form',
    container: '.card-wrapper',

    messages: {
        validDate: 'expire\ndate',
        monthYear: 'mm/yy'
    }
});
</script>
</body>
</html>

Using with jQuery

To use with jQuery, you'll need to include the jquery.card.js file into your HTML. You can find the necessary file at /dist/jquery.card.js and include it in your HTML like so.

<!-- at the end of BODY -->
<!-- CSS is included via this JavaScript file -->
<script src="/path/to/jquery.card.js"></script>

Once you've included those files, you can initialize Card with jQuery.

$('form').card({
    // a selector or DOM element for the container
    // where you want the card to appear
    container: '.card-wrapper', // *required*

    // all of the other options from above
});

Using with other javascript libraries

Card has wrappers that make it easy to use with other javascript libraries:

Angular 1.x

Angular 2+

Ember

React

Vue

For use with VueJs, install card.js from npm:

npm install card --save

Add in your component an Div with class 'card-wrapper', just pass in a selector that selects the fields in the correct order. Import the component card.js and add the object in instance mounted like this example:

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

<form>
    <input type="text" name="number">
    <input type="text" name="first-name"/>
    <input type="text" name="last-name"/>
    <input type="text" name="expiry"/>
    <input type="text" name="cvc"/>
</form>

<script>
import * as Card from "card";

export default {
    name: "Form CreditCard",
    mounted() {
    new Card({ 
      form: "form#cc-form",
      container: ".card-wrapper",
      formSelectors: { 
        numberInput: "input#cc-number",
        expiryInput: "input#cc-expiration",
        cvcInput: "input#cc-cvv",
        nameInput: "input#cc-name"
      },
      width: 270,
      formatting: true,
      placeholders: {
        number: "•••• •••• •••• ••••",
        name: "Nome Completo",
        expiry: "••/••",
        cvc: "•••"
      }
    });
  },
}
</script>

Development

To contribute, follow this steps:

$ git clone --recursive https://github.com/jessepollak/card.git
$ cd card
$ git submodule init && git submodule update
$ npm install
$ npm run development

Now, if you go to localhost:8080/example in your browser, you should see the demo page.

Places using Card

Card is used in the wild in these places:

Are you using Card in production? If so, we'd love to link to you from this page. Open a PR or drop @jessepollak a line on Twitter and we'll add you right away!

Project scope

The project scope is to improve the capture of payment cards on websites. Issues and fixes related to the user interface and validating of payment cards are in scope.

For questions on how to use Card in your particular project, please ask on Stack Overflow or similar forum.

Donations

If you'd like to donate to help support development of Card, send Bitcoin directly to 17NUKd3v7GWben18kGhmFafa4ZpWrXpQSC or through Coinbase here.

Comments
  • Remove jQuery dependence and offer a jQuery plugin in addition to normal library

    Remove jQuery dependence and offer a jQuery plugin in addition to normal library

    I'm not sure this nice lib really require jquery. It seems a good option to make something jquery free, then make available a jquery plugin (eg: Pikaday do that).

    enhancement 
    opened by MoOx 42
  • No longer works properly in Android browsers

    No longer works properly in Android browsers

    Hey Jesse – we've run into an issue that has put a major snafu into our mobile checkout pages.

    It appears that mobile Android browsers (we've tested Firefox, Chrome, and the standard Android browser on two Galaxy S5s and one Galaxy S6) no longer format or validate inputs properly. This occurs on both your demo page and within our site's implementation. We are able to input unlimited digits in the credit card number field, the expiration date field does not format properly (with the slash), the CVC number allows infinite digits, etc.

    It still works as expected on the two iOS devices we tested (both Safari and Chrome).

    An interesting find: when remote debugging via Chrome, inputting the values into the phone via desktop keyboard causes the inputs to behave as expected (formatting/input of slash/etc.), but the inputs still don't work when inputting text via the phone's keyboard.

    We received our first call about this issue yesterday, but it may have been occurring for much longer than that.

    bug 
    opened by johnbacon 30
  • Cannot read property 'getAttribute' of undefined

    Cannot read property 'getAttribute' of undefined

    I've tried using both the jquery.card.js and the card.js files. I've tried to include them at the bottom of the page. I am trying to use this in a modal so I am wondering if this is the issue. Here is my modal.

    and here is my script at the bottom of my php file.

    As mentioned in the title I get the Cannot read property 'getAttribute' of undefined in the console. Am I missing something?

    question 
    opened by SimplySynced 27
  • Support for ASP.net Web Forms

    Support for ASP.net Web Forms

    This this is awesome, great work.

    I cannot get the plugin to work with ASP.net web forms. I believe it has something to do with having the form tag on every page and not just around the form itself.

    opened by stanggt3 23
  • Handle non utf-8 browser rendering

    Handle non utf-8 browser rendering

    Hi,

    I tried this repository for my website. I used this:

    <!DOCTYPE html>
    <html lang="de"><head><style><endnote><head>
    <style><endnote><head>
    <style><endnote><head>
    <style><endnote><head>
    <style></style>
    <script></script><title>Card &#8212; the better way to collect credit cards</title>
    
    <link rel="stylesheet" href="http://suriyaakudo.bplaced.net/projects/card/lib/css/card.css">
    </head>
    <body>
    <style>
    .demo-container {
    width: 350px;
    margin: 50px auto;
    }
    form {
    margin: 30px;
    }
    input {
    width: 200px;
    margin: 10px auto;
    display: block;
    }
    </style>
    <div class="demo-container">
    <div class="card-wrapper"></div>
    <div class="form-container active">
    <form action=""><input placeholder="Card number" name="number" type="text"><input placeholder="Full name" name="name" type="text"><input placeholder="MM/YY" name="expiry" type="text"><input placeholder="CVC" name="cvc" type="text"></form>
    </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://suriyaakudo.bplaced.net/projects/card/lib/js/card.js"></script>
    <script>
    $('.active form').card({ container: $('.card-wrapper')})
    </script>
    </body></html>
    

    Where should I paste this:

    $('form').card({
        // a selector or jQuery object for the container
        // where you want the card to appear
        container: '.card-wrapper', // *required*
        numberInput: 'input#number', // optional — default input[name="number"]
        expiryInput: 'input#expiry', // optional — default input[name="expiry"]
        cvcInput: 'input#cvc', // optional — default input[name="cvc"]
        nameInput: 'input#name', // optional - defaults input[name="name"]
    
        width: 200, // optional — default 350px
        formatting: true // optional - default true
    });
    

    ???

    Yours Suriyaa Kudo

    bug 
    opened by suriyaa 19
  • Can't tab into CVC from Expiration Date.

    Can't tab into CVC from Expiration Date.

    I was just messing around with the input, and I tried to tab into the CVC field from the Expiration Date field, as many that are experienced with computers would, and the cursor remained inside the Expiration Date with no feedback.

    opened by bnb 17
  • Expiry date mask not working on mobile.

    Expiry date mask not working on mobile.

    Description

    Hi,

    I have the card working perfectly fine on Desktops (and even on the DevTools responsive layout tester). But going into a real mobile device, things don't work as expected.

    When placing the card data, everything works fine, the data goes into the card image, the card flips when focusing the CVC, and etc. But when I try to place the expiry date, it just don't mask the input.

    Example

    When I type "1121" I would expect "11 / 21", same as desktop, but it just keeps the same as what was typed:

    image

    (Of course that's not a real Credit Card, just to show that the data was typed before, but when testing with a real one, the error keeps the same)

    Code

    $("#payment").card({
        container: ".card-wrapper",
        formSelectors: {
          numberInput: "#card-number", 
          expiryInput: "#card-expiry",
          cvcInput: "#card-cvv", 
          nameInput: "#card-name", 
        },
        width: $(".card-wrapper").width(),
        placeholders: {
          number: "•••• •••• •••• ••••",
          name: "Name",
          expiry: "••/••",
          cvc: "•••",
        },
      });
    

    Looking at the DevTools (from the android tab), I got no console errors.

    Version

    I'm using the JQuery dist/jquery.card.js

    Conclusion

    Because it worked in Desktop, maybe there should be some compatibility error? Is there something that I can do to fix this?

    Thanks!

    invalid 
    opened by Enzodtz 16
  • Add new rebranding logo for issue #357

    Add new rebranding logo for issue #357

    There're three rebranded logos that plugin still using old logo yet: Visa, Mastercard, and Maestro. For these three, I just made some commits.

    This plugin is great by the way.

    Logo guideline reference: For Visa: https://usa.visa.com/dam/VCOM/download/merchants/New_VBM_Acq_Merchant_62714_v5.pdf For Mastercard and Maestro: https://brand.mastercard.com

    opened by iigmir 15
  • Not working properly at android mobile chrome

    Not working properly at android mobile chrome

    I am facing an issue when I am using the website on mobile device android google chrome. The card number input, expiry date, CVV all input fields neither formatting the inputs not validating them allow to the user to input string also at the place of digits.

    duplicate 
    opened by devqualwebs 13
  • Allow expiry inputs to work with a select dropdown

    Allow expiry inputs to work with a select dropdown

    Is there any way to make this work with a select dropdown for the expiration date? I have a form that I am trying to use this on but their expiration date is two separate select dropdowns (month/year).

    enhancement good-first-PR (easy) coffeescript 
    opened by justincron 13
  • Please Add JCB,Diners and UnionPay

    Please Add JCB,Diners and UnionPay

    Because asia region Most used brand. or already available? (I'm sorry, because it is Japanese, English is not well written.)

    JCB Logo:http://partner.jcb.jp/brand/images/logo_img01.jpg Diners Logo: https://www.diners.co.jp/ja/img/merchant/download/diners_logo_black.gif

    opened by ftkro 12
  • Credit Card with RTL the float:right makes a problem

    Credit Card with RTL the float:right makes a problem

    Hello everyone, Thanks for your efforts, I have a problem with the module when the direction of the page get rtl, the float:right in .jp-card .jp-card-front .jp-card-lower .jp-card-expiry, because the name also comes in the right, I tried this but didn't work in chrome: .jp-card .jp-card-front .jp-card-lower .jp-card-expiry[direction="rtl"] { float: left }

    what could be the solution?

    enhancement 
    opened by mfadel85 1
  • Fix donations link

    Fix donations link

    The donations link using Coinbase at the bottom of README.md is a broken link.

    Also, please consider to add another option for donations using fiat. GitHub has a nice program for this. I'd like to send money your way from my company's open source fund. But I can't deal in Bitcoin.

    Screen Shot 2020-12-01 at 10 11 16
    opened by fulldecent 4
  • Refreshing the Credit Card Image

    Refreshing the Credit Card Image

    I have the masked Credit Card No (**** **** **** 1234), expiry and Name stored on a cell phone. When the user wants to update their credit card information it would be nice if the form showed the currently saved values without having the user enter data in each field. So is there a way to cause the form to refresh using the values in the pre-loaded fields?

    workaround 
    opened by dcipher 9
Releases(2.5.4)
  • v.2.52(Apr 2, 2021)

  • v2.5.0(Jul 26, 2020)

  • v2.0.1(Jul 19, 2016)

  • v2.0.0(Jul 17, 2016)

    In addition to those code changes, with 2.0.0 we change how the code is built and packaged. There are now two different distribution folders:

    1. lib/ - this includes ES5 compatible code that can be built with anything that supports node style require statements (like browserify and webpack).
    2. dist/ - this includes browser compatible code that can be included in