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

ToRegexPattern()

To get a regex for an IBAN pattern, 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!