Credit Card Validation

Learn how to validate credit card numbers with JavaScript using card type detection and the Luhn algorithm.

Credit Card Validation

This page demonstrates card type detection and number validation using the Luhn algorithm. It is useful for checkout forms and payment demos.

Live Credit Card Demo

function luhnCheck(number) {
  let sum = 0, shouldDouble = false;
  for (let i = number.length - 1; i >= 0; i--) {
    let digit = parseInt(number[i], 10);
    if (shouldDouble) {
      digit *= 2;
      if (digit > 9) digit -= 9;
    }
    sum += digit;
    shouldDouble = !shouldDouble;
  }
  return sum % 10 === 0;
}

Card Validation Live Demo

Unknown