benjamincharity
7/31/2015 - 3:00 PM

Validate credit card with the luhn algorithm.

Validate credit card with the luhn algorithm.


/*
 * Check the validity of a credit card number
 *
 * https://gist.github.com/2134376
 * Phil Green (ShirtlessKirk)
 *
 * @param {String} number
 * @return {Bool} validity
 */
function luhnChk(number) {

    var len = number.length;
    var mul = 0;
    var prodArr = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]];
    var sum = 0;

    while (len--) {
        sum += prodArr[mul][parseInt(number.charAt(len), 10)];
        mul ^= 1;
    }

    return sum % 10 === 0 && sum > 0;

}