Skip to content

Commit b470166

Browse files
committed
autofill cc expiry
1 parent bc4ca02 commit b470166

2 files changed

Lines changed: 31 additions & 27 deletions

File tree

assets/js/expirationdate.js

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,49 @@
1-
function formatExpirationDate (val) {
1+
function formatExpirationDate(val) {
22
var p1 = parseInt(val[0], 10),
33
p2 = parseInt(val[1], 10);
44

55
return /^\d$/.test(val) && "0" !== val && "1" !== val
66
? "0" + val + " / "
77
: /^\d\d$/.test(val)
8-
? p2 > 2 && 0 !== p1 ? "0" + p1 + " / " + p2 : "" + val + " / "
9-
: val
8+
? p2 > 2 && 0 !== p1
9+
? "0" + p1 + " / " + p2
10+
: "" + val + " / "
11+
: val;
1012
}
1113

1214
function handleExpirationDate(element, focusOnFilled) {
13-
['input', 'keydown', 'keyup', 'mousedown', 'mouseup', 'select', 'contextmenu', 'drop'].forEach(function (key) {
14-
element.addEventListener(key, function (e) {
15-
if (/^\d+$/.test(e.key)) {
16-
this.value = formatExpirationDate(this.value);
17-
}
15+
["input", "keydown", "keyup", "mousedown", "mouseup", "select", "contextmenu", "drop"].forEach(
16+
function (key) {
17+
element.addEventListener(key, function (e) {
18+
if (/^\d+$/.test(e.key)) {
19+
this.value = formatExpirationDate(this.value);
20+
}
1821

19-
if (/^\d{0,2}[\/]?\d{0,2}/.test(this.value)) {
20-
this.oldValue = this.value;
21-
this.oldSelectionStart = this.selectionStart;
22-
this.oldSelectionEnd = this.selectionEnd;
23-
} else if (this.hasOwnProperty('oldValue')) {
24-
this.value = this.oldValue;
25-
this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
26-
} else {
27-
this.value = '';
28-
}
29-
if (this.oldSelectionEnd === 7 && key === 'keyup' && e.key.match(/^[0-9]$/)) {
30-
secureFields.focus(focusOnFilled);
31-
}
32-
});
33-
});
22+
if (/^\d{0,2}[\/]?\d{0,2}/.test(this.value)) {
23+
this.oldValue = this.value;
24+
this.oldSelectionStart = this.selectionStart;
25+
this.oldSelectionEnd = this.selectionEnd;
26+
} else if (this.hasOwnProperty("oldValue")) {
27+
this.value = this.oldValue;
28+
this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
29+
} else {
30+
this.value = "";
31+
}
32+
if (this.oldSelectionEnd === 7 && key === "keyup" && e?.key?.match(/^[0-9]$/)) {
33+
secureFields.focus(focusOnFilled);
34+
}
35+
});
36+
}
37+
);
3438
}
3539

36-
function getExpirationDateObject (value) {
37-
var split = value.split('/')
40+
function getExpirationDateObject(value) {
41+
var split = value.split("/");
3842
expm = split[0].trim();
3943
expy = split[1].trim();
4044

4145
return {
4246
expm: expm,
43-
expy: expy
47+
expy: expy,
4448
};
4549
}

pciproxy-examples/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<label for="expiry">Expiration Date</label>
5050
<div class="secure-field--input-container" style="height: 40px">
5151
<input id="expiry" type="tel" maxlength="7" placeholder="MM / YY" class="secure-field--input"
52-
style="height: 40px; outline: none; font-size: 100%" autocomplete="off" />
52+
style="height: 40px; outline: none; font-size: 100%" autocomplete="cc-exp" />
5353
<div class="secure-field--actions">
5454
<span class="secure-field--action secure-field--action__card-icon">
5555
<span class="secure-field--action-card-wrap">

0 commit comments

Comments
 (0)