jQuery.payment
A general purpose library for building credit card forms, validating inputs and formatting numbers.
Project status
We consider jQuery.payment
to be feature complete. We continue to use it in production, and we will happily accept bug reports and pull requests fixing those bugs, but we will not be adding new features or modifying the project for new frameworks or build systems.
Why?
The library was born in a different age, and we think it has served tremendously, but it is fundamentally doing too many things. Complecting DOM element manipulation, input masking, card formatting, and cursor positioning makes it difficult to test and modify. An ideal version of this library would separate the independent components and make the internal logic functional.
Usage
You can make an input act like a credit card field (with number formatting and length restriction):
;
Then, when the payment form is submitted, you can validate the card number on the client-side:
var valid = $payment;if !valid;return false;
You can find a full demo here.
Supported card types are:
- Visa
- MasterCard
- American Express
- Diners Club
- Discover
- UnionPay
- JCB
- Maestro
- Forbrugsforeningen
- Dankort
(Additional card types are supported by extending the $.payment.cards
array.)
API
$.fn.payment('formatCardNumber')
Formats card numbers:
- Includes a space between every 4 digits
- Restricts input to numbers
- Limits to 16 numbers
- Supports American Express formatting
- Adds a class of the card type (e.g. 'visa') to the input
Example:
;
$.fn.payment('formatCardExpiry')
Formats card expiry:
- Includes a
/
between the month and year - Restricts input to numbers
- Restricts length
Example:
;
$.fn.payment('formatCardCVC')
Formats card CVC:
- Restricts length to 4 numbers
- Restricts input to numbers
Example:
;
$.fn.payment('restrictNumeric')
General numeric input restriction.
Example:
;
$.payment.validateCardNumber(number)
Validates a card number:
- Validates numbers
- Validates Luhn algorithm
- Validates length
Example:
$payment; //=> true
$.payment.validateCardExpiry(month, year)
Validates a card expiry:
- Validates numbers
- Validates in the future
- Supports year shorthand
Example:
$payment; //=> true$payment; //=> true$payment; //=> false
$.payment.validateCardCVC(cvc, type)
Validates a card CVC:
- Validates number
- Validates length to 4
Example:
$payment; //=> true$payment; //=> true$payment; //=> true$payment; //=> false
$.payment.cardType(number)
Returns a card type. Either:
visa
mastercard
amex
dinersclub
discover
unionpay
jcb
maestro
forbrugsforeningen
dankort
The function will return null
if the card type can't be determined.
Example:
$payment; //=> 'visa'
$.payment.cardExpiryVal(string) and $.fn.payment('cardExpiryVal')
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
).
$payment; //=> {month: 3, year: 2025}$payment; //=> {month: 5, year: 2004}//=> {month: 4, year: 2020}
This function doesn't perform any validation of the month or year; use $.payment.validateCardExpiry(month, year)
for that.
$.payment.cards
Array of objects that describe valid card types. Each object should contain the following fields:
// Card type, as returned by $.payment.cardType.type: 'mastercard'// Array of prefixes used to identify the card type.patterns:51 52 53 54 5522 23 24 25 26 27// 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 prefix in patterns
. For this reason, patterns with higher specificity should appear towards the beginning of the array.
Example
Look in ./example/index.html
Building
Run cake build
Running tests
Run cake test
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 mark up your fields using the Autofill spec. These are respected by a number of browsers, including Chrome, Safari, Firefox.
Mobile recommendations
We recommend you to use <input type="tel">
which will cause the numeric keyboard to be displayed on mobile devices: