-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCCNumberInput.js
284 lines (240 loc) · 8.2 KB
/
CCNumberInput.js
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
var React = require('react');
var cards = require('./cards');
var Util = require('./Util');
var utils = new Util();
var CCNumberInput = React.createClass({
// # Utils
cardFromNumber: function(num) {
num = (num + '').replace(/\D/g, '');
// return card for card in cards when card.pattern.test(num)
// console.log(num);
for(var i = 0; i < cards.length; i++) {
var card = cards[i];
if(card.pattern.test(num)) {
return card;
}
}
return null;
},
restrictCardNumber: function(e) {
console.log('restrictCardNumber');
var target = e.target,
digit = String.fromCharCode(e.which);
if(!/^\d+$/.test(digit)) return true;
if(utils.hasTextSelected(target)) return true;
// # Restrict number of digits
var value = (target.value + digit).replace(/\D/g, ''),
card = this.cardFromNumber(value);
if(card) {
// if(value.length <= card.length[card.length.length - 1])
if(value.length > card.length[card.length.length - 1]) {
e.preventDefault();
return false;
}
} else {
// # All other cards are 16 digits long
// if(value.length <= 16)
if(value.length > 16) {
e.preventDefault();
return false;
}
}
return true;
},
getCardType: function(num) {
var card = this.cardFromNumber(num);
if(card) {
return card.type;
} else {
return 'unknown';
}
},
setCardType: function(e) {
var target = e.target,
val = target.value,
cardType = this.getCardType(val);
this.setState({cardType: cardType});
},
getFormattedCardNumber: function(num) {
num = num.replace(/\D/g, '');
var card = this.cardFromNumber(num);
if(!card) return num;
var upperLength = card.length[card.length.length - 1];
num = num.slice(0, upperLength);//num[0...upperLength]
if(card.format.global) {
// num.match(card.format)?.join(' ');
var _ref;
var retVal = (_ref = num.match(card.format)) != null ? _ref.join(' ') : null;
// console.log('getFormattedCardNumber'); console.log(retVal);
return retVal;
}
else {
var groups = card.format.exec(num);
if(!groups) return;// unless groups?
groups.shift();
// console.log(groups, ' pre-filter');
groups = groups.filter(function(n) { return n; });// # Filter empty groups
// console.log(groups, ' post-filter');
// groups = $.grep(groups, (n) -> n);// # Filter empty groups
console.log('getFormattedCardNumber()', groups.join(' '));
return groups.join(' ');
}
},
reFormatCardNumber: function(e) {
var target = e.target,
value = target.value;
value = this.getFormattedCardNumber(value);
console.log('reFormatCardNumber', value);
this.setState({value: value});
// $target.val(value);
},
formatCardNumber: function(e) {
console.log('formatCardNumber');
// # Only format if input is a number
var digit = String.fromCharCode(e.which);
if(!/^\d+$/.test(digit)) return;
var target = e.target, // $(e.currentTarget)
value = target.value,
card = this.cardFromNumber(value + digit),
length = (value.replace(/\D/g, '') + digit).length;
var upperLength = 16;
if(card) {
upperLength = card.length[card.length.length - 1];
}
console.log(length, '>=', upperLength);
if(length >= upperLength) {
console.log('true');
return;
}
// console.log('target.selectionStart');
// console.log(target.selectionStart);
// # Return if focus isn't at the end of the text
if(target.selectionStart && target.selectionStart != value.length) {
console.log('focus not at start of text');
return;
}
var re;
if(card && card.type == 'amex') {
// # AMEX cards are formatted differently
re = /^(\d{4}|\d{4}\s\d{6})$/;
console.log('amex format');
} else {
console.log('not amex format');
re = /(?:^|\s)(\d{4})$/;
}
// # If '4242' + 4
if(re.test(value)) {
e.preventDefault();
// setTimeout -> $target.val(value + ' ' + digit)
this.setState({value: value + ' ' + digit});
// # If '424' + 2
} else if(re.test(value + digit)) {
e.preventDefault();
// setTimeout -> $target.val(value + digit + ' ');
this.setState({value: value + digit + ' '});
}
},
// TODO: issues with deleting '34' from Amex card values that start with 34567
formatBackCardNumber: function (e) {
var target = e.target,
value = target.value;
// # Return unless backspacing
if (e.which != 8) return;
// # Return if focus isn't at the end of the text
if (target.selectionStart != null && target.selectionStart != value.length) {
// console.log('formatBackCardNumber: focus not at end of text');
return;
}
// # Remove the digit + trailing space
if (/\d\s$/.test(value)) {
e.preventDefault();
// console.log('formatBackCardNumber 1: ', '"' + value.replace(/\d\s$/, '') + '"');
this.setState({value: value.replace(/\d\s$/, '') });
// setTimeout -> $target.val(value.replace(/\d\s$/, ''));
} else if (/\s\d?$/.test(value)) {
// # Remove digit if ends in space + digit
e.preventDefault();
// console.log('formatBackCardNumber 2: ', '"' + value.replace(/\d$/, '') + '"');
this.setState({value: value.replace(/\d$/, '') });
// setTimeout -> $target.val(value.replace(/\d$/, ''));
}
},
getInitialState: function() {
return {
value: '',
cardType: 'unknown'
};
},
componentDidMount: function() {
// emitter.addListener('onKeyPress', function(e) {
// console.log('onKeyPress');
// console.log(this);
// console.log(e);
// e.stopPropagation();
// // this.restrictNumeric();
// });
// emitter.addListener('onKeyPress', this.restrictCardNumber);
// emitter.addListener('onKeyPress', this.formatCardNumber);
//
// console.log('this.refs.ccInput');
// console.log(this.refs.ccInput);
// this.refs.ccInput.on('keyPress', function() { console.log('!!! ccInput.on !!!'); });
},
reFormatNumeric: function(e) {
console.log('reFormatNumeric : value', e.target.value);
var value = utils.reFormatNumeric(e);
console.log('reFormatNumeric : setState(value)', value);
this.setState({value: value });
},
onKeyPress: function(e) {
console.log('CCInput -> onKeyPress');
// console.log(e);
// emitter.emit('onKeyPress', e);
if(!utils.restrictNumeric(e)) { return false; }
// this.reFormatNumeric(e);
// if(!this.restrictCardNumber(e)) { console.log('restrictCardNumber: return false'); return false; }
this.restrictCardNumber(e);
this.formatCardNumber(e);
},
onKeyDown: function(e) {
console.log('CCInput -> onKeyDown');
this.formatBackCardNumber(e);
},
onKeyUp: function(e) {
// console.log('CCInput -> onKeyUp');
this.setCardType(e);
},
onPaste: function(e) {
// console.log('CCInput -> onPaste');
this.reFormatNumeric(e);
this.reFormatCardNumber(e);
},
onChange: function(event) {
console.log('CCInput -> onChange');
// console.log(event);
// this.setState({value: event.target.value});
},
onInput: function(e) {
console.log('CCInput -> onInput');
this.reFormatNumeric(e);
this.reFormatCardNumber(e);
this.setCardType(e);
},
render: function() {
var value = this.state.value,
cardType = this.state.cardType;
return (
<div>
<input className="cc-number" ref="ccInput" type="tel" value={value} placeholder="•••• •••• •••• ••••"
autoComplete="cc-number"
onKeyPress={this.onKeyPress}
onKeyDown={this.onKeyDown}
onKeyUp={this.onKeyUp}
onPaste={this.onPaste}
onChange={this.onChange}
onInput={this.onInput} /> {cardType}
</div>
);
}
});
module.exports = CCNumberInput;