Skip to content
Martijn Bodeman edited this page Oct 11, 2024 · 9 revisions

IBAN regex patterns

To get a regex for an IBAN pattern (per country), use the ToRegexPattern() extension:

Example

foreach (var country in IbanRegistry.Default)
{
    var regex = country.Iban.Pattern.ToRegexPattern();
    Console.WriteLine($"{country.TwoLetterISORegionName}: {regex}");
}

IbanNet internally does not use any regex itself, instead it uses dedicated parsing/validation logic tuned for performance.

A regex can be useful in non-.NET/client side code like a browser as a first line of defense. By using the regex specifically provided by IbanNet, it is guaranteed that the client side validation would be consistent with the backend validation, because the regex is directly derived from the IbanRegistry. Of course, validation based on a regex is not foolproof, because it does not perform check digit validation, nor provide any context on which part of an IBAN is invalid. So always perform backend validation!

QR-IBAN

After parsing an IBAN into the primitive type Iban, use the IsQrIban() extension to determine whether the IBAN is a valid QR-IBAN from a Swiss or Liechtenstein account.

A valid QR-IBAN must have a valid QR-IID, i.e. the bank number must be within the [30000, 31999] range (both ends inclusive).

Example

Iban iban = new IbanParser(IbanRegistry.Default).Parse("CH54 3100 0111 1111 1111 1");
Console.Writeline(iban.IsQrIban()); // true

Alternatively, you can also register the rule QrIbanRule in the IbanValidator. Once the rule is added, validation will fail if the input is not a QR-IBAN.

Example

var validator = new IbanValidator(IbanRegistry.Default, [new QrIbanRule()]);
Console.Writeline(validator.Validate("CH53 3200 0000 0000 0000 0").IsValid); // false

The QrIbanRule by definition also blocks (!) IBAN's from any country other than Switzerland and Liechtenstein.