public static CreditCardTypeId GetCardTypeFromNumber2(string cardNum) { // Compare the supplied card number with the regex pattern and get reference regex named groups // NOTE: this does not mean the card is valid format/length, just that it resembles a type. // Assume clean cardNum. GroupCollection gc = _cardRegex1.Value.Match(cardNum).Groups; for (CreditCardTypeId id = CreditCardTypeId.Visa; id <= CreditCardTypeId.DinersClub; id++) { // Compare each card type to the named groups to determine which card type the number matches if (gc[id.ToString()].Success) { return(id); } } // Card type is not supported by our system, return null return(CreditCardTypeId.Unk); }
public static bool IsValidNumber2(string cardNum, CreditCardTypeId cardType) { // Assume clean cardNum. if (!IsValidType(cardType)) { return(false); } if (!IsValidLength(cardNum.Length, cardType)) { return(false); } // Make sure the supplied number matches the supplied card type if (!_cardRegex1.Value.Match(cardNum).Groups[cardType.ToString()].Success) { return(false); // The card number does not match the card type } // all cards use Luhn's alg return(IsValidLuhn2(cardNum)); }