Class CardNumberExtensions
- Namespace
- HeroKit.Banking
- Assembly
- HeroKit.dll
Contains extension methods for validating payment card numbers and detecting the card brand.
public static class CardNumberExtensions
- Inheritance
-
CardNumberExtensions
- Inherited Members
Remarks
Spaces and dashes are ignored. Any other non-digit character makes the number invalid. The checks cover format and the Luhn checksum. They do not confirm that a card exists.
Methods
GetCardBrand(string?)
Detects the brand of a payment card from its prefix and length.
public static CardBrand GetCardBrand(this string? value)
Parameters
valuestringThe card number. Spaces and dashes are ignored.
Returns
Examples
"4111111111111111".GetCardBrand(); // CardBrand.Visa
"378282246310005".GetCardBrand(); // CardBrand.AmericanExpress
Remarks
The Luhn check is not required here. Use IsValidCardNumber(string?) for that.
IsValidCardNumber(string?)
Determines whether a string is a valid payment card number.
public static bool IsValidCardNumber(this string? value)
Parameters
valuestringThe card number to check. Spaces and dashes are ignored.
Returns
- bool
true if the number has 12 to 19 digits and passes the Luhn check; otherwise, false. Null or empty input returns false.
Examples
"4111 1111 1111 1111".IsValidCardNumber(); // true
"4111111111111112".IsValidCardNumber(); // false