Skip to content
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).

Example

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);
}

Error results

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

Example

ValidationResult validationResult = validator.Validate(..);
if (validationResult.Error is InvalidLengthResult)
{
    .. 
}