-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCCVInput.js
56 lines (46 loc) · 1.2 KB
/
CCVInput.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
var React = require('react');
var Util = require('./Util');
var utils = new Util();
var CCVInput = React.createClass({
getInitialState: function() {
return {
value: ''
};
},
restrictCVC: function(e) {
var target = e.target,
digit = String.fromCharCode(e.which);
if(!/^\d+$/.test(digit)) return;
if(utils.hasTextSelected(target)) return;
var value = target.value + digit;
if(value.length > 4) {
e.preventDefault();
}
},
reFormatCVC: function(e) {
var target = e.target,
value = target.value;
value = value.replace(/\D/g, '').slice(0, 4); //[0...4]
this.setState({value: value});
},
onKeyPress: function(e) {
if(!utils.restrictNumeric(e)) { return false; }
this.restrictCVC(e);
},
onPaste: function(e) {
this.reFormatCVC(e);
},
onInput: function(e) {
this.reFormatCVC(e);
},
render: function() {
var value = this.state.value;
return (
<input className="cc-ccv" ref="ccVInput" type="tel" value={value} autoComplete="off" placeholder="•••"
onKeyPress={this.onKeyPress}
onPaste={this.onPaste}
onInput={this.onInput} />
);
}
});
module.exports = CCVInput;