isValidCardLength function
Checks to see whether the input number is of the correct length, given the assumed brand of the card. This function does not perform a Luhn check.
@param cardNumber the card number with no spaces or dashes @param cardBrand a {@link CardBrand} used to get the correct size @return {@code true} if the card number is the correct length for the assumed brand
Implementation
bool isValidCardLength(String? cardNumber, {String? cardBrand}) {
cardBrand ??= getPossibleCardType(cardNumber, shouldNormalize: false);
if (cardNumber == null || StripeCard.unknown == cardBrand) {
return false;
}
int length = cardNumber.length;
switch (cardBrand) {
case StripeCard.americanExpress:
return length == LENGTH_AMERICAN_EXPRESS;
case StripeCard.dinersClub:
return length == LENGTH_DINERS_CLUB;
default:
return length == LENGTH_COMMON_CARD;
}
}