Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 55 additions & 49 deletions lib/angular-payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,35 +161,35 @@ angular.module('angularPayments', []);angular.module('angularPayments')

var _hasTextSelected = function($target) {
var ref;

if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== $target.prop('selectionEnd')) {
return true;
}

if (typeof document !== "undefined" && document !== null ? (ref = document.selection) != null ? typeof ref.createRange === "function" ? ref.createRange().text : void 0 : void 0 : void 0) {
return true;
}

return false;
};

// card formatting

var _formatCardNumber = function(e) {
var $target, card, digit, length, re, upperLength, value;

digit = String.fromCharCode(e.which);
$target = angular.element(e.currentTarget);
value = $target.val();
card = Cards.fromNumber(value + digit);
length = (value.replace(/\D/g, '') + digit).length;

upperLength = 16;

if (card) {
upperLength = card.length[card.length.length - 1];
}

if (length >= upperLength) {
return;
}
Expand Down Expand Up @@ -221,21 +221,21 @@ angular.module('angularPayments', []);angular.module('angularPayments')

var _restrictCardNumber = function(e) {
var $target, card, digit, value;

$target = angular.element(e.currentTarget);
digit = String.fromCharCode(e.which);

if(!/^\d+$/.test(digit)) {
return;
}

if(_hasTextSelected($target)) {
return;
}

value = ($target.val() + digit).replace(/\D/g, '');
card = Cards.fromNumber(value);

if(card) {
if(!(value.length <= card.length[card.length.length - 1])){
e.preventDefault();
Expand All @@ -249,22 +249,22 @@ angular.module('angularPayments', []);angular.module('angularPayments')

var _formatBackCardNumber = function(e) {
var $target, value;

$target = angular.element(e.currentTarget);
value = $target.val();

if(e.meta) {
return;
}

if(e.which !== 8) {
return;
}

if(($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) {
return;
}

if(/\d\s$/.test(value) && !e.meta && e.keyCode >= 46) {
e.preventDefault();
return $target.val(value.replace(/\d\s$/, ''));
Expand All @@ -276,22 +276,22 @@ angular.module('angularPayments', []);angular.module('angularPayments')

var _getFormattedCardNumber = function(num) {
var card, groups, upperLength, ref;

card = Cards.fromNumber(num);

if (!card) {
return num;
}

upperLength = card.length[card.length.length - 1];
num = num.replace(/\D/g, '');
num = num.slice(0, +upperLength + 1 || 9e9);

if(card.format.global) {
return (ref = num.match(card.format)) != null ? ref.join(' ') : void 0;
} else {
groups = card.format.exec(num);

if (groups != null) {
groups.shift();
}
Expand All @@ -304,7 +304,7 @@ angular.module('angularPayments', []);angular.module('angularPayments')
return setTimeout(function() {
var $target, value;
$target = angular.element(e.target);

value = $target.val();
value = _getFormattedCardNumber(value);
return $target.val(value);
Expand All @@ -331,14 +331,19 @@ angular.module('angularPayments', []);angular.module('angularPayments')
_formatCVC = function(e){
$target = angular.element(e.currentTarget);
digit = String.fromCharCode(e.which);

if (!/^\d+$/.test(digit) && !e.meta && e.keyCode >= 46) {

// Allows backspace, delete and arrow keys when input length is 4 and the browser is Firefox
if (e.keyCode == 46 || e.keyCode == 8||(e.keyCode >=37 && e.keyCode <=40)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do you know that it is Firefox here?

Copy link
Author

@andyhsh andyhsh Jun 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@raphaeldieu IE / Chrome / Safari detects the "keypress" event only when an actual character input is added. Since backspace and arrow keys don't add a new character, the function doesn't run.

For Firefox, the "keypress" event is run even on backspace / arrow keys and so with the old code when val.length is more than 4, it will always fall down to the else block which has a e.preventDefault() preventing the user from any form of input. The new code which detects arrow / delete / backspace keys therefore can only occur on firefox

return;
}

if (!/^\d+$/.test(digit) && !e.meta && e.which >= 46) {
e.preventDefault();
return;
}

val = $target.val() + digit;

if(val.length <= 4){
return;
} else {
Expand All @@ -355,22 +360,22 @@ angular.module('angularPayments', []);angular.module('angularPayments')

_restrictExpiry = function(e) {
var $target, digit, value;

$target = angular.element(e.currentTarget);
digit = String.fromCharCode(e.which);

if (!/^\d+$/.test(digit) && !e.meta && e.keyCode >= 46) {
e.preventDefault();
return;
}

if(_hasTextSelected($target)) {
return;
}

value = $target.val() + digit;
value = value.replace(/\D/g, '');

if (value.length > 6) {
e.preventDefault()
return;
Expand All @@ -379,17 +384,17 @@ angular.module('angularPayments', []);angular.module('angularPayments')

_formatExpiry = function(e) {
var $target, digit, val;

digit = String.fromCharCode(e.which);

if (!/^\d+$/.test(digit) && !e.meta && e.keyCode >= 46) {
e.preventDefault();
return;
}

$target = angular.element(e.currentTarget);
val = $target.val() + digit;

if (/^\d$/.test(val) && (val !== '0' && val !== '1')) {
e.preventDefault();
return $target.val("0" + val + " / ");
Expand All @@ -403,56 +408,56 @@ angular.module('angularPayments', []);angular.module('angularPayments')

_formatForwardExpiry = function(e) {
var $target, digit, val;

digit = String.fromCharCode(e.which);

if (!/^\d+$/.test(digit) && !e.meta && e.keyCode >= 46) {
return;
}

$target = angular.element(e.currentTarget);
val = $target.val();

if (/^\d\d$/.test(val)) {
return $target.val("" + val + " / ");
}
};

_formatForwardSlash = function(e) {
var $target, slash, val;

slash = String.fromCharCode(e.which);

if (slash !== '/') {
return;
}

$target = angular.element(e.currentTarget);
val = $target.val();

if (/^\d$/.test(val) && val !== '0') {
return $target.val("0" + val + " / ");
}
};

_formatBackExpiry = function(e) {
var $target, value;

if (e.meta) {
return;
}

$target = angular.element(e.currentTarget);
value = $target.val();

if (e.which !== 8) {
return;
}

if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) {
return;
}

if (/\d(\s|\/)+$/.test(value)) {
e.preventDefault();
return $target.val(value.replace(/\d(\s|\/)*$/, ''));
Expand Down Expand Up @@ -517,7 +522,8 @@ angular.module('angularPayments', []);angular.module('angularPayments')
_Format(attr.paymentsFormat, elem, ctrl);
}
}
}]);angular.module('angularPayments')
}])
;angular.module('angularPayments')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ran grunt and I guess it changed the line. Didn't directly edit anything inside the /lib folder




Expand Down
Loading