You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1022 B
40 lines
1022 B
2 years ago
|
import assertString from './util/assertString';
|
||
|
/* eslint-disable max-len */
|
||
|
|
||
|
var creditCard = /^(?:4[0-9]{12}(?:[0-9]{3,6})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12,15}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11}|6[27][0-9]{14}|^(81[0-9]{14,17}))$/;
|
||
|
/* eslint-enable max-len */
|
||
|
|
||
|
export default function isCreditCard(str) {
|
||
|
assertString(str);
|
||
|
var sanitized = str.replace(/[- ]+/g, '');
|
||
|
|
||
|
if (!creditCard.test(sanitized)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
var sum = 0;
|
||
|
var digit;
|
||
|
var tmpNum;
|
||
|
var shouldDouble;
|
||
|
|
||
|
for (var i = sanitized.length - 1; i >= 0; i--) {
|
||
|
digit = sanitized.substring(i, i + 1);
|
||
|
tmpNum = parseInt(digit, 10);
|
||
|
|
||
|
if (shouldDouble) {
|
||
|
tmpNum *= 2;
|
||
|
|
||
|
if (tmpNum >= 10) {
|
||
|
sum += tmpNum % 10 + 1;
|
||
|
} else {
|
||
|
sum += tmpNum;
|
||
|
}
|
||
|
} else {
|
||
|
sum += tmpNum;
|
||
|
}
|
||
|
|
||
|
shouldDouble = !shouldDouble;
|
||
|
}
|
||
|
|
||
|
return !!(sum % 10 === 0 ? sanitized : false);
|
||
|
}
|