Skip to content

Instantly share code, notes, and snippets.

@johan
Created January 23, 2017 22:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johan/0f96d330ca8cb83dbe0665790ce13155 to your computer and use it in GitHub Desktop.
Save johan/0f96d330ca8cb83dbe0665790ce13155 to your computer and use it in GitHub Desktop.
A small ES6 credit card number validation and formatting library based on https://github.com/omarshammas/jquery.formance/blob/master/src/zfields/credit_card_number.coffee
// Card.validate(n), Card.getType(n), Card.format(n)
const Card = ((cards) => {
const cardFromNumber = (n) => {
n = (n + '').replace(/\D/g, '');
for (let i = 0; i < cards.length; i++) {
const card = cards[i];
if (card.pattern.test(n)) {
return card;
}
}
};
const getType = (n) => {
const card = cardFromNumber(n);
return card && card.type;
};
const defaultFormat = /(\d{1,4})/g;
const format = (n) => {
let card = cardFromNumber(n = n + ''), maxLen, groups;
if (!card) return n;
maxLen = card.length[card.length.length - 1];
n = n.replace(/\D+/g, '').slice(0, +maxLen + 1);
const re = card.format || defaultFormat;
if (re.global) {
groups = n.match(re);
} else {
groups = re.exec(n);
if (groups != null) groups.shift();
}
return groups && groups.join(' ');
};
const checkLuhn = (n) => {
let sum = 0;
for (let odd = 1, digits = n.split('').reverse(), i = 0; i < digits.length; i++) {
let digit = parseInt(digits[i], 10);
if ((odd = !odd)) digit *= 2;
if (digit > 9) digit -= 9;
sum += digit;
}
return !(sum % 10);
};
const isNumeric = (n) => /^\d+$/.test(n);
const validate = (n, cvc = '') => {
n = (n + '').replace(/\s+|-/g, '');
if (!isNumeric(n)) return { type: '?', error: 'card number not numeric' };
const card = cardFromNumber(n);
if (!card) return { type: '?', error: 'card type not recognized' };
const {type} = card;
if (card.length.indexOf(n.length) < 0)
return { type, error: 'invalid card number length' };
if (card.luhn && !checkLuhn(n))
return { type, error: 'invalid card number' };
if (cvc) {
if (!isNumeric(cvc = cvc + ''))
return { type, error: 'card cvc not numeric' };
if (card.cvcLength.indexOf(cvc.length) < 0)
return { type, error: 'invalid cvc length' };
}
};
return { validate, getType, format };
})(
[ { type: 'amex'
, pattern: /^3[47]/
, format: /(\d{1,4})(\d{1,6})?(\d{1,5})?/
, length: [15]
, cvcLength: [3, 4]
, luhn: true
}
, { type: 'dinersclub'
, pattern: /^(36|38|30[0-5])/
, length: [14]
, cvcLength: [3]
, luhn: true
}
, { type: 'discover'
, pattern: /^(6011|65|64[4-9]|622)/
, length: [16]
, cvcLength: [3]
, luhn: true
}
, { type: 'jcb'
, pattern: /^35/
, length: [16]
, cvcLength: [3]
, luhn: true
}
, { type: 'laser'
, pattern: /^(6706|6771|6709)/
, length: [16, 17, 18, 19]
, cvcLength: [3]
, luhn: true
}
, { type: 'maestro'
, pattern: /^(5018|5020|5038|6304|6759|676[1-3])/
, length: [12, 13, 14, 15, 16, 17, 18, 19]
, cvcLength: [3]
, luhn: true
}
, { type: 'mastercard'
, pattern: /^5[1-5]/
, length: [16]
, cvcLength: [3]
, luhn: true
}
, { type: 'unionpay'
, pattern: /^62/
, length: [16, 17, 18, 19]
, cvcLength: [3]
, luhn: false
}
, { type: 'visa'
, pattern: /^4/
, length: [13, 14, 15, 16]
, cvcLength: [3]
, luhn: true
}
]);
Copyright (c) 2013 Omar Shammas (omar@shammaslabs.com)
and Stripe (alex@stripe.com)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment