My Finance Library contains some utilities for finance-related operations. Its purpose is to gather various common finance operations so that developers do not have to rewrite them every time.
The library can be obtained by installing the Dandago.Finance package via Nuget. Source code is available at BitBucket.
Features
- IBAN validation
- Credit card number validation
- Diner’s Club / Carte Blanche
- American Express
- VISA
- MasterCard
- Discover
- Luhn algorithm (used in credit card validation, but can be used on its own as well)
Prerequisites
Any framework compatible with .NET Standard 1.2. So .NET Framework 4.5.1+, .NET Core 1.0+, etc.
Example Usages
Installing Dandago.Finance
First, install Dandago.Finance. You can do this either via the NuGet GUI:
..or via the NuGet Package Manager Console:
Install-Package Dandago.Finance
Then just add the following statement to be able to access the library’s classes:
using Dandago.Finance;
Validating IBANs
Just pass the IBAN number to IbanValidator.IsValidIban():
// valid IBAN bool valid1 = IbanValidator.IsValidIban("AL47212110090000000235698741"); // invalid IBAN bool valid2 = IbanValidator.IsValidIban("ABCDEF");
Validating Credit Cards, and Luhn Algorithm
The CreditCardValidator provides static methods to validate the various supported credit card number types:
bool validDinersClub = CreditCardValidator.IsValidDinersClub("30569309025904"); bool validAmericanExpress = CreditCardValidator.IsValidAmericanExpress("378282246310005"); bool validVisa = CreditCardValidator.IsValidVisa("4111111111111111"); bool validMasterCard = CreditCardValidator.IsValidMasterCard("5555555555554444"); bool validDiscover = CreditCardValidator.IsValidDiscover("6011111111111117");
Each of the above checks does the following:
- Basic checks (e.g. empty string)
- Structural check (based on the format of the credit card number)
- Luhn algorithm to verify checksum
If you want to check whether a credit card number is valid (based on the supported types), but don’t care about the card type itself, you should use CreditCardValidator.IsValidCreditCard(), which performs #1 and #3 only once, but performs #2 per card type:
bool validWhatever = CreditCardValidator.IsValidCreditCard("5555555555554444");
The Luhn algorithm is used to validate credit card number checksums. If you want to use it separately (e.g. to validate an unsupported credit card type), you can:
bool passesLuhn = LuhnAlgorithm.IsValid("5555555555554444");
Useful Links
- International Bank Account Number – Validating the IBAN
- IBAN Registry [PDF]
- How to validate credit card number?
- Test Credit Card Account Numbers
- Luhn Algorithm
- Validate credit card number with Mod 10 algorithm
- Bank card number
Release History
12th July 2015 – initial release on NuGet. Includes IBAN validator and credit card validator, the latter covering five major credit cards as well as the Luhn algorithm.
23rd March 2017 – ported to .NET Standard.
One thought on “Finance Library”