validateCardNum static method

String? validateCardNum(
  1. String input
)

With the card number with Luhn Algorithm https://en.wikipedia.org/wiki/Luhn_algorithm

Implementation

static String? validateCardNum(String input) {
  if (input.isEmpty) {
    return UIStrings.fieldReq;
  }

  input = getCleanedNumber(input);

  if (input.length < 8) {
    return UIStrings.numberIsInvalid;
  }

  int sum = 0;
  int length = input.length;
  for (var i = 0; i < length; i++) {
    // get digits in reverse order
    int digit = int.parse(input[length - i - 1]);

    // every 2nd number multiply with 2
    if (i % 2 == 1) {
      digit *= 2;
    }
    sum += digit > 9 ? (digit - 9) : digit;
  }

  if (sum % 10 == 0) {
    return null;
  }

  return UIStrings.numberIsInvalid;
}