-
Notifications
You must be signed in to change notification settings - Fork 33
Validating an IBAN
skwasjer edited this page Jan 13, 2020
·
14 revisions
Use the IbanValidator
for full control over the validation.
The benefit of using the validator is that it implements the IIbanValidator
interface and can thus be easily mocked. Additionally, the ValidationResult
provides extra context, like the matched country (if any).
IIbanValidator validator = new IbanValidator();
ValidationResult validationResult = validator.Validate("NL91ABNA041716430");
if (validationResult.IsValid)
{
// For example:
if (validationResult.Country.TwoLetterISORegionName != "NL")
{
throw new InvalidOperationException("Please provide a Dutch bank account.");
}
}
else
{
Console.WriteLine(validationResult.Error.ErrorMessage);
}
When an IBAN is invalid, the error is reported in the ValidationResult.Error
property. Below a list of possible built-in errors:
IllegalCharactersResult
IllegalCountryCodeCharactersResult
InvalidCheckDigitsResult
InvalidLengthResult
InvalidStructureResult
UnknownCountryCodeResult
ValidationResult validationResult = validator.Validate(..);
if (validationResult.Error is InvalidLengthResult)
{
..
}