Skip to content

Commit c821d8f

Browse files
committed
README revisions
1 parent e8d1c47 commit c821d8f

File tree

2 files changed

+95
-63
lines changed

2 files changed

+95
-63
lines changed

CONTRIBUTING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ problem is before you spend your valuable time trying to fix it.
2020
Again, guard your time and effort. Make sure that you don't spend a lot
2121
of time on an improvement without talking through it first.
2222

23+
## Getting to work
24+
25+
```sh
26+
npm install
27+
npm run build
28+
npm test
29+
```
30+
2331
## Pull Requests
2432

2533
**Make sure to send pull requests to `develop`.**

README.md

Lines changed: 87 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,7 @@
77

88
A general purpose library for building credit card forms, validating inputs, and formatting numbers.
99

10-
Available via **NPM** (Node or Browserify) and **Bower**. Supports IE8+.
11-
12-
For example, you can make a input act like a credit card field (with number formatting and length restriction):
13-
14-
``` javascript
15-
var input = document.getElementById('ccnum');
16-
payform.cardNumberInput(input);
17-
```
18-
19-
Then, when the payment form is submitted, you can validate the card number on the client-side (or server-side):
20-
21-
``` javascript
22-
var valid = payform.validateCardNumber(input.value);
23-
24-
if (!valid) {
25-
alert('Your card is not valid!');
26-
return false;
27-
}
28-
```
29-
30-
You can find a [demo here](http://jondavidjohn.github.io/payform).
31-
32-
Supported card types are:
10+
Supported card types:
3311

3412
* Visa
3513
* MasterCard
@@ -43,11 +21,58 @@ Supported card types are:
4321
* Forbrugsforeningen
4422
* Dankort
4523

46-
(Additional card types are supported by extending the [`payform.cards`](#payformcards) array.)
24+
(Custom card types are [supported](#custom-cards))
25+
26+
Works in IE8+ and all other modern browsers.
27+
28+
[**Demo**](http://jondavidjohn.github.io/payform)
29+
30+
## Installation / Usage
31+
32+
### NPM (Node and Browserify)
33+
34+
```sh
35+
npm install payform --save
36+
```
37+
38+
```javascript
39+
var payform = require('payform');
40+
41+
// Validate a credit card number
42+
payform.validateCardNumber('4242 4242 4242 4242'); //=> true
43+
44+
// Get card type from number
45+
payform.parseCardType('4242 4242 4242 4242'); //=> 'visa'
46+
```
47+
48+
[Full API Doc](#api)
49+
50+
### Direct script include / Bower
51+
52+
Optionally via bower (or simply via download)
53+
```
54+
bower install payform --save
55+
```
56+
57+
```html
58+
<script src="dist/payform.js"></script>
59+
<script>
60+
// Validate a credit card number
61+
payform.validateCardNumber('4242 4242 4242 4242'); //=> true
62+
63+
// Get card type from number
64+
payform.parseCardType('4242 4242 4242 4242'); //=> 'visa'
65+
</script>
66+
```
67+
68+
[Full API Doc](#api)
69+
4770

4871
## API
4972

50-
### payform.validateCardNumber(number)
73+
### General Formatting and Validation
74+
75+
#### payform.validateCardNumber(number)
5176

5277
Validates a card number:
5378

@@ -61,7 +86,7 @@ Example:
6186
payform.validateCardNumber('4242 4242 4242 4242'); //=> true
6287
```
6388

64-
### payform.validateCardExpiry(month, year)
89+
#### payform.validateCardExpiry(month, year)
6590

6691
Validates a card expiry:
6792

@@ -77,7 +102,7 @@ payform.validateCardExpiry('05', '2015'); //=> true
77102
payform.validateCardExpiry('05', '05'); //=> false
78103
```
79104

80-
### payform.validateCardCVC(cvc, type)
105+
#### payform.validateCardCVC(cvc, type)
81106

82107
Validates a card CVC:
83108

@@ -93,7 +118,7 @@ payform.validateCardCVC('1234', 'amex'); //=> true
93118
payform.validateCardCVC('12344'); //=> false
94119
```
95120

96-
### payform.parseCardType(number)
121+
#### payform.parseCardType(number)
97122

98123
Returns a card type. Either:
99124

@@ -115,9 +140,10 @@ Example:
115140

116141
``` javascript
117142
payform.parseCardType('4242 4242 4242 4242'); //=> 'visa'
143+
payform.parseCardType('hello world?'); //=> null
118144
```
119145

120-
### payform.parseCardExpiry(string)
146+
#### payform.parseCardExpiry(string)
121147

122148
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`).
123149

@@ -128,35 +154,11 @@ payform.parseCardExpiry('05 / 04'); //=> {month: 5, year: 2004}
128154

129155
This function doesn't perform any validation of the month or year; use `payform.validateCardExpiry(month, year)` for that.
130156

131-
### payform.cards
132-
133-
Array of objects that describe valid card types. Each object should contain the following fields:
134-
135-
``` javascript
136-
{
137-
// Card type, as returned by payform.parseCardType.
138-
type: 'mastercard',
139-
// Regex used to identify the card type. For the best experience, this should be
140-
// the shortest pattern that can guarantee the card is of a particular type.
141-
pattern: /^5[0-5]/,
142-
// Array of valid card number lengths.
143-
length: [16],
144-
// Array of valid card CVC lengths.
145-
cvcLength: [3],
146-
// Boolean indicating whether a valid card number should satisfy the Luhn check.
147-
luhn: true,
148-
// Regex used to format the card number. Each match is joined with a space.
149-
format: /(\d{1,4})/g
150-
}
151-
```
152-
153-
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.
154-
155-
## Browser `<input>` Helpers
157+
### Browser `<input>` formatting helpers
156158

157159
These methods are specifically for use in the browser to attach `<input>` formatters.
158160

159-
### payform.cardNumberInput(input)
161+
#### payform.cardNumberInput(input)
160162

161163
Formats card numbers:
162164

@@ -172,7 +174,7 @@ var input = document.getElementById('ccnum');
172174
payform.cardNumberInput(input);
173175
```
174176

175-
### payform.expiryInput(input)
177+
#### payform.expiryInput(input)
176178

177179
Formats card expiry:
178180

@@ -183,11 +185,11 @@ Formats card expiry:
183185
Example:
184186

185187
``` javascript
186-
var input = document.getElementById('ccnum');
188+
var input = document.getElementById('expiry');
187189
payform.expiryInput(input);
188190
```
189191

190-
### payform.cvcInput(input)
192+
#### payform.cvcInput(input)
191193

192194
Formats card CVC:
193195

@@ -197,17 +199,39 @@ Formats card CVC:
197199
Example:
198200

199201
``` javascript
200-
var input = document.getElementById('ccnum');
202+
var input = document.getElementById('cvc');
201203
payform.cvcInput(input);
202204
```
203205

204-
## Building
206+
### Custom Cards
207+
208+
#### payform.cards
205209

206-
Run `npm run build`
210+
Array of objects that describe valid card types. Each object should contain the following fields:
211+
212+
``` javascript
213+
{
214+
// Card type, as returned by payform.parseCardType.
215+
type: 'mastercard',
216+
// Regex used to identify the card type. For the best experience, this should be
217+
// the shortest pattern that can guarantee the card is of a particular type.
218+
pattern: /^5[0-5]/,
219+
// Array of valid card number lengths.
220+
length: [16],
221+
// Array of valid card CVC lengths.
222+
cvcLength: [3],
223+
// Boolean indicating whether a valid card number should satisfy the Luhn check.
224+
luhn: true,
225+
// Regex used to format the card number. Each match is joined with a space.
226+
format: /(\d{1,4})/g
227+
}
228+
```
229+
230+
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.
207231

208-
## Running tests
232+
## Development
209233

210-
Run `npm test`
234+
Please see [CONTRIBUTING.md](https://github.com/jondavidjohn/payform/blob/develop/CONTRIBUTING.md).
211235

212236
## Autocomplete recommendations
213237

0 commit comments

Comments
 (0)