-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.html
202 lines (174 loc) · 5.57 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<!DOCTYPE html>
<html lang="en">
<head>
<title>payment-cards.js</title>
<meta charset="utf8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* {
box-sizing: border-box;
}
html, body {
background-color: #fff;
color: #000;
font-family: Verdana;
font-size: 16px;
}
body {
overflow-y: scroll;
}
.container {
margin: 0 auto;
max-width: 780px;
}
noscript {
background-color: #f2dede;
color: #555;
display: block;
font-weight: bold;
padding: 0.5em 0.8em;
}
.warning {
background-color: #fcf8e3;
color: #555;
display: block;
font-weight: bold;
padding: 0.5em 0.8em;
}
pre {
font-size: 14px;
}
.payment-card {
background-color: #eaeaea;
border: 1px solid #ddd;
border-radius: 8px;
height: 171px;
margin-bottom: 1em;
padding-top: 42px;
position: relative;
width: 270px;
}
.payment-card input {
width: 100%;
}
.pan {
margin-bottom: 10px;
padding: 0 15px;
}
.expiry, .cvn {
float: left;
padding: 0 15px;
width: 50%;
}
.brand {
color: #888;
padding: 3px 6px;
position: absolute;
right: 15px;
top: 5px;
}
.brand:empty {
display: none;
}
input.abnormal {
box-shadow: #faebcc 0 0 2px 2px;
}
input.invalid {
box-shadow: red 0 0 1.5px 1px;
}
input.valid {
box-shadow: green 0 0 1.5px 1px;
}
input:focus {
box-shadow: none;
}
</style>
</head>
<body>
<div class="container">
<h1>payment-cards.js</h1>
<p>payment-cards.js provides formatting, validation, and brand detection for payment card forms. For the documentation see <a href="https://github.com/liberapay/payment-cards.js">the repository on GitHub</a>.</p>
<h2>Demo</h2>
<noscript>JavaScript is required</noscript>
<p class="warning">You should avoid inputting real card data in this form.</p>
<form id="card" action="javascript:" method="POST">
<div class="payment-card">
<div class="pan">
<input id="pan" class="form-control" required autocomplete="cc-number"
placeholder="Card number" />
</div>
<div class="expiry">
<input id="expiry" class="form-control" required autocomplete="cc-exp"
placeholder="MM/YY" />
</div>
<div class="cvn">
<input id="cvn" class="form-control" required autocomplete="cc-csc"
placeholder="CVN" />
</div>
<div class="brand"></div>
</div>
<pre id="json-dump"></pre>
</form>
</div>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script src="payment-cards.js"></script>
<script>
var cardForm;
function clearInvalid($form) {
$form.find('.invalid').removeClass('invalid');
$form.find('.abnormal').removeClass('abnormal');
};
function setValidity($input, validity) {
$input.toggleClass('invalid', validity == 'invalid');
$input.toggleClass('abnormal', validity == 'abnormal');
};
function updateValidity(card) {
clearInvalid($('form#card'));
setValidity($('input#pan'), card.pan.status || 'abnormal');
setValidity($('input#expiry'), card.expiry.status);
setValidity($('input#cvn'), card.cvn.status || 'valid');
}
function dump(card) {
function replacer(key, value) {
if (value instanceof RegExp) {
return value.toString();
}
return value;
}
var r = JSON.stringify(card, replacer, 4);
return r.replace(/\[(\s+\d+,)*(\s+\d+)?\s*\]/g, function (m) {
return m.replace(/\s+/g, ' ');
});
}
function updateOutput(card) {
$('.brand').text(card.brand);
$('pre#json-dump').text('card = ' + dump(card));
}
$(function() {
$('input[title]').each(function () {
var $input = $(this);
$('<span class="tooltip">').text($input.attr('title')).insertAfter($input);
});
$('form#card').submit(function (e) { e.preventDefault(); return false; })
cardForm = new PaymentCards.Form(
document.querySelector('input#pan'),
document.querySelector('input#expiry'),
document.querySelector('input#cvn')
);
function onBlur() {
updateValidity(cardForm.check());
}
cardForm.inputs.pan.addEventListener('blur', onBlur);
cardForm.inputs.expiry.addEventListener('blur', onBlur);
cardForm.inputs.cvn.addEventListener('blur', onBlur);
function onInput() {
updateOutput(cardForm.check());
}
cardForm.inputs.pan.addEventListener('input', onInput);
cardForm.inputs.expiry.addEventListener('input', onInput);
cardForm.inputs.cvn.addEventListener('input', onInput);
onInput();
});
</script>
</body>
</html>