Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jondavidjohn/payform
:credit_card: A library for building credit card forms, validating inputs, and formatting numbers.
https://github.com/jondavidjohn/payform
Last synced: 2 days ago
JSON representation
:credit_card: A library for building credit card forms, validating inputs, and formatting numbers.
- Host: GitHub
- URL: https://github.com/jondavidjohn/payform
- Owner: jondavidjohn
- License: other
- Created: 2015-01-29T18:27:04.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2022-06-22T02:27:21.000Z (over 2 years ago)
- Last Synced: 2024-10-20T03:23:50.846Z (15 days ago)
- Language: CoffeeScript
- Homepage: https://jondavidjohn.github.io/payform/
- Size: 432 KB
- Stars: 427
- Watchers: 14
- Forks: 81
- Open Issues: 20
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- starred-awesome - payform - :credit_card: A library for building credit card forms, validating inputs, and formatting numbers. (CoffeeScript)
README
---
Do you use webpack?
Wish your team made reducing the size of your webpack builds a priority? Want to know how the changes you're making impact your asset profile for every pull request?
Check it out at packtracker.io.---
# payform[![Build Status](https://travis-ci.org/jondavidjohn/payform.svg?branch=master)](https://travis-ci.org/jondavidjohn/payform)
![Dependencies](https://david-dm.org/jondavidjohn/payform.svg)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](#custom-cards))
Works in IE8+ and all other modern browsers.
[**Demo**](https://jondavidjohn.github.io/payform)
## Installation / Usage
### npm (Node and Browserify)
```sh
npm install payform --save
``````javascript
var payform = require('payform');// Format input for card number entry
var input = document.getElementById('ccnum');
payform.cardNumberInput(input)// Validate a credit card number
payform.validateCardNumber('4242 4242 4242 4242'); //=> true// Get card type from number
payform.parseCardType('4242 4242 4242 4242'); //=> 'visa'
```### AMD / Require.js
```javascript
require.config({
paths: { "payform": "path/to/payform" }
});require(["payform"], function (payform) {
// Format input for card number entry
var input = document.getElementById('ccnum');
payform.cardNumberInput(input)// Validate a credit card number
payform.validateCardNumber('4242 4242 4242 4242'); //=> true// Get card type from number
payform.parseCardType('4242 4242 4242 4242'); //=> 'visa'
});
```### Direct script include / Bower
Optionally via bower (or simply via download)
```sh
bower install payform --save
``````html
// Format input for card number entry
var input = document.getElementById('ccnum');
payform.cardNumberInput(input)// Validate a credit card number
payform.validateCardNumber('4242 4242 4242 4242'); //=> true// Get card type from number
payform.parseCardType('4242 4242 4242 4242'); //=> 'visa'```
### 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.](#browser-input-formatting-helpers)```html
// Format input for card number entry
$('input.ccnum').payform('formatCardNumber');// Validate a credit card number
$.payform.validateCardNumber('4242 4242 4242 4242'); //=> true// Get card type from number
$.payform.parseCardType('4242 4242 4242 4242'); //=> 'visa'```
## API
### General Formatting and Validation
#### payform.validateCardNumber(number)
Validates a card number:
* Validates numbers
* Validates Luhn algorithm
* Validates lengthExample:
``` javascript
payform.validateCardNumber('4242 4242 4242 4242'); //=> true
```#### payform.validateCardExpiry(month, year)
Validates a card expiry:
* Validates numbers
* Validates in the future
* Supports year shorthandExample:
``` javascript
payform.validateCardExpiry('05', '20'); //=> true
payform.validateCardExpiry('05', '2015'); //=> true
payform.validateCardExpiry('05', '05'); //=> false
```#### payform.validateCardCVC(cvc, type)
Validates a card CVC:
* Validates number
* Validates length to 4Example:
``` javascript
payform.validateCardCVC('123'); //=> true
payform.validateCardCVC('123', 'amex'); //=> true
payform.validateCardCVC('1234', 'amex'); //=> true
payform.validateCardCVC('12344'); //=> 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:
``` javascript
payform.parseCardType('4242 4242 4242 4242'); //=> 'visa'
payform.parseCardType('hello world?'); //=> 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`).
``` javascript
payform.parseCardExpiry('03 / 2025'); //=> {month: 3: year: 2025}
payform.parseCardExpiry('05 / 04'); //=> {month: 5, year: 2004}
```This function doesn't perform any validation of the month or year; use `payform.validateCardExpiry(month, year)` for that.
### Browser `` formatting helpers
These methods are specifically for use in the browser to attach `` formatters.
(alternate [jQuery Plugin](#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 formattingExample:
``` javascript
var input = document.getElementById('ccnum');
payform.cardNumberInput(input);
```#### payform.expiryInput(input)
_jQuery plugin:_ `$(...).payform('formatCardExpiry')`
Formats card expiry:
* Includes a `/` between the month and year
* Restricts input to numbers
* Restricts lengthExample:
``` javascript
var input = document.getElementById('expiry');
payform.expiryInput(input);
```#### payform.cvcInput(input)
_jQuery plugin:_ `$(...).payform('formatCardCVC')`
Formats card CVC:
* Restricts length to 4 numbers
* Restricts input to numbersExample:
``` javascript
var input = document.getElementById('cvc');
payform.cvcInput(input);
```#### payform.numericInput(input)
_jQuery plugin:_ `$(...).payform('formatNumeric')`
General numeric input restriction.
Example:
``` javascript
var input = document.getElementById('numeric');
payform.numericInput(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 `` formatters.
#### payform.detachCardNumberInput(input)
_jQuery plugin:_ `$(...).payform('detachFormatCardNumber')`
Example:
``` javascript
var input = document.getElementById('ccnum');
// now you're able to detach:
payform.detachCardNumberInput(input);
```#### payform.detachExpiryInput(input)
_jQuery plugin:_ `$(...).payform('detachFormatCardExpiry')`
Example:
``` javascript
var input = document.getElementById('expiry');
payform.expiryInput(input);
// now you're able to detach:
payform.detachExpiryInput(input);
```#### payform.detachCvcInput(input)
_jQuery plugin:_ `$(...).payform('detachFormatCardCVC')`
Example:
``` javascript
var input = document.getElementById('cvc');
payform.cvcInput(input);
// now you're able to detach:
payform.detachCvcInput(input);
```#### payform.detachNumericInput(input)
_jQuery plugin:_ `$(...).payform('detachFormatNumeric')`
Example:
``` javascript
var input = document.getElementById('numeric');
payform.numericInput(input);
// now you're able to detach:
payform.detachNumericInput(input);
```### Custom Cards
#### payform.cards
Array of objects that describe valid card types. Each object should contain the following fields:
``` javascript
{
// 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: /(\d{1,4})/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](https://github.com/jondavidjohn/payform/blob/develop/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:
``` html
```
You should also mark up your fields using the [Autofill spec](https://html.spec.whatwg.org/multipage/forms.html#autofill). These are respected by a number of browsers, including Chrome.
``` html
```
Set `autocomplete` to `cc-number` for credit card numbers and `cc-exp` for credit card expiry.
## Mobile recommendations
We recommend you to use `` which will cause the numeric keyboard to be displayed on mobile devices:
``` html
```
## A derived work
This library is derived from a lot of great work done on [`jquery.payment`](https://github.com/stripe/jquery.payment) by the folks at [Stripe](https://stripe.com/). This aims to
build upon that work, in a module that can be consumed in more diverse situations.