Skip to content
This repository was archived by the owner on Feb 10, 2026. It is now read-only.

Commit 13d1862

Browse files
committed
Improve expiry separator behavior
1 parent c9cc514 commit 13d1862

3 files changed

Lines changed: 59 additions & 2 deletions

File tree

lib/angular-payments.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,29 @@ angular.module('angularPayments', []);angular.module('angularPayments')
454454
return;
455455
}
456456

457+
function reformatExpiry(value) {
458+
var newVal = '',
459+
index = 0;
460+
461+
for (var i = 0; i < value.length; i++) {
462+
if (value[i] !== ' ' && value[i] !== '/') {
463+
newVal = newVal + value[i];
464+
index++;
465+
if (index === 2) {
466+
newVal = newVal += ' / ';
467+
}
468+
}
469+
}
470+
471+
return newVal;
472+
}
473+
457474
// Is control character (arrow keys, delete, enter, etc...)
458475
function isSystemKey(code) {
459476
return code === 8 || code === 0 || code === 13
460477
}
461478

479+
// reformat a badly formed expiry
462480
// Allow normal system keys to work
463481
if (isSystemKey(e.which) || e.metaKey) {
464482
return
@@ -467,6 +485,14 @@ angular.module('angularPayments', []);angular.module('angularPayments')
467485
$target = angular.element(e.currentTarget);
468486
val = $target.val() + digit;
469487

488+
// handle the user messing up the separator
489+
if (val.length > 2 && val.indexOf('/') < 0) {
490+
// separator has been deleted
491+
$target.val(val.substring(0, 2) + ' / ' + val.substring(2));
492+
} else if ( val.length > 2 && val.indexOf('/') !== 3) {
493+
$target.val(reformatExpiry(val));
494+
}
495+
470496
if (/^\d$/.test(val) && (val !== '0' && val !== '1')) {
471497
e.preventDefault();
472498
return $target.val("0" + val + " / ");
@@ -476,6 +502,8 @@ angular.module('angularPayments', []);angular.module('angularPayments')
476502
return $target.val("" + val + " / ");
477503

478504
}
505+
506+
479507
};
480508

481509
_formatForwardExpiry = function(e) {

0 commit comments

Comments
 (0)