validateCreditCardNumber method

bool validateCreditCardNumber(
  1. String creditCard
)

Implementation

bool validateCreditCardNumber(String creditCard) {
  var reg = RegExp(r"(/[^0-9-\s]+/)");

  if (0 == creditCard.length || reg.hasMatch(creditCard)) return false;

  creditCard = creditCard.replaceAll(RegExp(r"[^\d]"), "");

  var t = 0;
  var a = false;

  for (var r = creditCard.length - 1; r >= 0; r--) {
    var i = creditCard[r];
    var n = int.parse(i);

    if (a && (n *= 2) > 9 && (n -= 9) == 0) {
      t += n;
    } else {
      a = !a;
    }
  }
  return t % 10 == 0;
}