Skip to content
Martijn Bodeman edited this page Jun 21, 2024 · 44 revisions

The default options are fine for most use cases. IbanNet however does have a limited set of options:

Registry

IbanNet is by default using rules as defined by the SWIFT registry to validate strictly according to the ISO 13616 standard. SWIFT is the official registration authority for national IBAN formats.

Disclaimer: IbanNet is not endorsed or sponsored by SWIFT, or in any way affiliated. This library merely uses the specification as provided publicly by SWIFT.

The (default) registry containing the SWIFT specification can be used in code by accessing the static IbanRegistry.Default instance. It represents a dictionary of specifications keyed by country code.

Other registry providers

IbanNet also supplies a registry provider based on data from Wikipedia. The patterns defined on Wikipedia however are less exact in comparison to SWIFT, and some information is missing. This library only offers this provider as an alternative or supplement, and should only be used in cases where you really need have support for countries that are listed on Wikipedia, but not officially acknowledged by SWIFT.

Configure (any) additional providers through the options, after adding the primary SwiftRegistryProvider:

var validator = new IbanValidator(new IbanValidatorOptions
{
    Registry = new IbanRegistry
    {
        Providers =
        {
            new SwiftRegistryProvider(),   // Preferred
            new WikipediaRegistryProvider()  // If country not available in SWIFT, use Wikipedia.
        }
    }
});

If multiple providers are registered, IbanNet will pick the ruleset for a specific country from the first provider that supports the given country.

Roll your own IIbanRegistryProvider

If you wish to extend or override validation patterns defined by the built-in providers, implement IIbanRegistryProvider. For examples of such an implementation, you can look at the source code of the built-in providers.

Custom post-validation rules

To apply additional validation logic, implement IIbanValidationRule and a custom error result. Custom rules will always be executed 'after' built-in validation has succeeded, so in that respect you can be sure the IBAN is valid when the rule executes. What extra logic is applied is up to you.

Use cases could be to verify banks, branch codes, etc.:

class MyCustomEror : ErrorResult
{
    public MyCustomEror()
        : base($"Something was wrong.")
    {
    }
}

class MyCustomRule : IIbanValidationRule
{
    public ValidationRuleResult Validate(ValidationRuleContext context)
    {
        if (context.Value == "xxx")
        {
            // Return custom error if not valid.
            return new MyCystomError();
        }

        // Success.
        return ValidationRuleResult.Success;
    }
}

Register the rule through the options:

var validator = new IbanValidator(new IbanValidatorOptions
{
    Rules =
    {
        new MyCustomRule()
    }
});

IbanNet also has a few built-in additional rules that are not active by default, but you can register in the same way:

  • AcceptCountryRule accepts IBANs from specific countries and rejects all others.
  • RejectCountryRule rejects IBANs from specific countries and accepts all others.
  • QrIbanRule verifies that IBANs are valid (Swiss) QR IBANs.