Validate credit card numbers with Luhn check, detect card brand, and mask for display.
dotnet add package Philiprehberger.CreditCardValidatorValidate credit card numbers with Luhn check, detect card brand, and mask for display.
dotnet add package Philiprehberger.CreditCardValidator
using Philiprehberger.CreditCardValidator;
// Validate a card number
bool isValid = CreditCard.IsValid("4111111111111111"); // true
// Detect brand
CardBrand brand = CreditCard.DetectBrand("4111111111111111"); // Visa
// Mask for display
string masked = CreditCard.Mask("4111111111111111"); // "************1111"
// Format with spaces
string formatted = CreditCard.Format("4111111111111111"); // "4111 1111 1111 1111"
// Full validation
CardValidation result = CreditCard.Validate("4111111111111111");
// result.IsValid == true, result.Brand == CardBrand.Visa
CreditCard| Method | Description |
|---|---|
IsValid(string number) | Returns true if the number passes the Luhn check. |
DetectBrand(string number) | Returns the CardBrand based on the BIN prefix. |
Mask(string number, char maskChar = '*') | Masks all but the last 4 digits. |
Format(string number) | Formats the number with spaces in groups of 4. |
Validate(string number) | Returns a CardValidation with validity, brand, and errors. |
CardBrandEnum: Unknown, Visa, MasterCard, Amex, Discover, DinersClub, JCB, UnionPay.
CardValidationRecord: IsValid (bool), Brand (CardBrand), Errors (string[]).
LuhnValidator| Method | Description |
|---|---|
IsValid(string digits) | Luhn mod-10 check. Ignores spaces and dashes. |
BrandDetector| Method | Description |
|---|---|
Detect(string number) | Detects brand from BIN (first 6 digits). |
dotnet build src/Philiprehberger.CreditCardValidator.csproj --configuration Release
If you find this project useful: