index.js
/**
* Verify if a given value is a valid credit card number.
*
* @public
* @since 0.1.0
* @param {!String} value - The value to be validated.
* @return {Boolean}
*
* @example
* var isCCValid = defy.creditcard('378282246310005')
*/
Verify if a given value is a valid credit card number.
Verify if a given value is a valid credit card number .
function creditcard (value) {
  if (!cardDigitsRegEx.test(value)) {
    return false
  }
  value = value.replace(/\D/g, '')
  if (value.length < 12 || value.length > 19) {
    return false
  }
  let nCheck = 0
  let nDigit = 0
  let bEven = false
  let cDigit
  for (let n = value.length - 1; n >= 0; n--) {
    cDigit = value.charAt(n)
    nDigit = parseInt(cDigit, 10)
    if (bEven) {
      if ((nDigit *= 2) > 9) nDigit -= 9
    }
    nCheck += nDigit
    bEven = !bEven
  }
  return (nCheck % 10) === 0
}
function creditcard ( value ) { if ( ! cardDigitsRegEx . test ( value ) ) { return false } value = value . replace ( / \D / g , '' ) if ( value . length < 12 || value . length > 19 ) { return false } let nCheck = 0 let nDigit = 0 let bEven = false let cDigit for ( let n = value . length - 1 ; n >= 0 ; n -- ) { cDigit = value . charAt ( n ) nDigit = parseInt ( cDigit , 10 ) if ( bEven ) { if ( ( nDigit *= 2 ) > 9 ) nDigit -= 9 } nCheck += nDigit bEven = ! bEven } return ( nCheck % 10 ) === 0 }
Verify if a given value is a valid credit card number.
Verify if a given value is a valid credit card number .
// Accept only digits, optionally in groups separated by a single dash or space // Luhn Algorithm