Hi! Do you use webpack?
I'm currently working on a product that will help you keep track of the assets you're producing and their delivery performance concerns. Along with reporting those stats right to your GitHub pull requests.
Check it out at packtracker.io and request early access if you want to help me kick the tires!
payform
A general purpose library for building credit card forms, validating inputs, and formatting numbers.
Supported card types:
- Visa
- MasterCard
- American Express
- Diners Club
- Discover
- UnionPay
- JCB
- Visa Electron
- Maestro
- Forbrugsforeningen
- Dankort
(Custom card types are also supported)
Works in IE8+ and all other modern browsers.
Installation / Usage
npm (Node and Browserify)
npm install payform --save
var payform = ; // Format input for card number entryvar input = document;payform // Validate a credit card numberpayform; //=> true // Get card type from numberpayform; //=> 'visa'
AMD / Require.js
require; ;
Direct script include / Bower
Optionally via bower (or simply via download)
bower install payform --save
jQuery Plugin (also supports Zepto)
This library also includes a jquery plugin. The primary payform
object
can be found at $.payform
, and there are jquery centric ways to utilize the browser
input formatters.
API
General Formatting and Validation
payform.validateCardNumber(number)
Validates a card number:
- Validates numbers
- Validates Luhn algorithm
- Validates length
Example:
payform; //=> true
payform.validateCardExpiry(month, year)
Validates a card expiry:
- Validates numbers
- Validates in the future
- Supports year shorthand
Example:
payform; //=> truepayform; //=> truepayform; //=> false
payform.validateCardCVC(cvc, type)
Validates a card CVC:
- Validates number
- Validates length to 4
Example:
payform; //=> truepayform; //=> truepayform; //=> truepayform; //=> false
payform.parseCardType(number)
Returns a card type. Either:
visa
mastercard
amex
dinersclub
discover
unionpay
jcb
visaelectron
maestro
forbrugsforeningen
dankort
The function will return null
if the card type can't be determined.
Example:
payform; //=> 'visa'payform; //=> null
payform.parseCardExpiry(string)
Parses a credit card expiry in the form of MM/YYYY, returning an object containing the month
and year
. Shorthand years, such as 13
are also supported (and converted into the longhand, e.g. 2013
).
payform; //=> {month: 3: year: 2025}payform; //=> {month: 5, year: 2004}
This function doesn't perform any validation of the month or year; use payform.validateCardExpiry(month, year)
for that.
<input>
formatting helpers
Browser These methods are specifically for use in the browser to attach <input>
formatters.
(alternate jQuery Plugin syntax is also provided)
payform.cardNumberInput(input)
jQuery plugin: $(...).payform('formatCardNumber')
Formats card numbers:
- Includes a space between every 4 digits
- Restricts input to numbers
- Limits to 16 numbers
- Supports American Express formatting
Example:
var input = document;payform;
payform.expiryInput(input)
jQuery plugin: $(...).payform('formatCardExpiry')
Formats card expiry:
- Includes a
/
between the month and year - Restricts input to numbers
- Restricts length
Example:
var input = document;payform;
payform.cvcInput(input)
jQuery plugin: $(...).payform('formatCardCVC')
Formats card CVC:
- Restricts length to 4 numbers
- Restricts input to numbers
Example:
var input = document;payform;
payform.numericInput(input)
jQuery plugin: $(...).payform('formatNumeric')
General numeric input restriction.
Example:
var input = document;payform;
<input>
Detaching formatting helpers from Once you have used the formatting helpers available, you might also want to remove them from your input elements. Being able to remove them is especially useful in a Single Page Application (SPA) environment where you want to make sure you're properly unsubscribing events from elements before removing them from the DOM. Detaching events will assure you will not encounter any memory leaks while using this library.
These methods are specifically for use in the browser to detach <input>
formatters.
payform.detachCardNumberInput(input)
jQuery plugin: $(...).payform('detachFormatCardNumber')
Example:
var input = document;// now you're able to detach:payform;
payform.detachExpiryInput(input)
jQuery plugin: $(...).payform('detachFormatCardExpiry')
Example:
var input = document;payform;// now you're able to detach:payform;
payform.detachCvcInput(input)
jQuery plugin: $(...).payform('detachFormatCardCVC')
Example:
var input = document;payform;// now you're able to detach:payform;
payform.detachNumericInput(input)
jQuery plugin: $(...).payform('detachFormatNumeric')
Example:
var input = document;payform;// now you're able to detach:payform;
Custom Cards
payform.cards
Array of objects that describe valid card types. Each object should contain the following fields:
// Card type, as returned by payform.parseCardType.type: 'mastercard'// Regex used to identify the card type. For the best experience, this should be// the shortest pattern that can guarantee the card is of a particular type.pattern: /^5[0-5]/// Array of valid card number lengths.length: 16// Array of valid card CVC lengths.cvcLength: 3// Boolean indicating whether a valid card number should satisfy the Luhn check.luhn: true// Regex used to format the card number. Each match is joined with a space.format: //g
When identifying a card type, the array is traversed in order until the card number matches a pattern
. For this reason, patterns with higher specificity should appear towards the beginning of the array.
Development
Please see CONTRIBUTING.md.
Autocomplete recommendations
We recommend you turn autocomplete on for credit card forms, except for the CVC field (which should never be stored). You can do this by setting the autocomplete
attribute:
You should also mark up your fields using the Autofill spec. These are respected by a number of browsers, including Chrome.
Set autocomplete
to cc-number
for credit card numbers and cc-exp
for credit card expiry.
Mobile recommendations
We recommend you to use <input type="tel">
which will cause the numeric keyboard to be displayed on mobile devices:
A derived work
This library is derived from a lot of great work done on jquery.payment
by the folks at Stripe. This aims to
build upon that work, in a module that can be consumed in more diverse situations.