# validation.bic
Checks that a Business Identifier Code - the "SWIFT code" of a bank - is well
formed, and splits it into its four parts. `DEUTDEFF500` is Deutsche Bank
(`DEUT`), Germany (`DE`), Frankfurt (`FF`), branch `500`.
FORMAT, as the ISO 20022 payment messages validate it (the 2014 edition of
ISO 9362, pattern `[A-Z0-9]{4}[A-Z]{2}[A-Z0-9]{2}([A-Z0-9]{3})?`):
- 4 letters or digits: the business party prefix (institution). Every BIC
issued so far uses letters here, but ISO 9362:2014 allows digits, and
rejecting them would make this stricter than the payment messages it guards.
- 2 letters: the country, an ISO 3166-1 alpha-2 code (plus `XK`, which SWIFT
assigned to Kosovo).
- 2 letters or digits: the location. A `0` in the second place conventionally
marks a test BIC; this capability does not flag it, and it is still a
well-formed code.
- 3 letters or digits, optional: the branch. An 8-character BIC means the
primary office and is the same as the 11-character form ending `XXX`, so
`branch` is `XXX` for both. `normalised` keeps the length the caller gave,
because some systems require one form or the other.
A WELL-FORMED BIC IS NOT A REGISTERED ONE. Only SWIFT's BIC directory can say
that the code is assigned and still active. The country letters are checked to
be letters, not looked up in the ISO 3166 list, which would need data and
releases of its own; pair this with a country check if that matters.
ACCEPTED INPUT: letters in either case (folded to upper case) and digits, with
ASCII spaces ignored anywhere ("DEUT DE FF"). Hyphens and other punctuation are
refused.
| reason | meaning |
|-----------------|------------------------------------------------------|
| `empty` | nothing but spaces, or not a string at all |
| `bad-character` | anything other than A-Z, a-z, 0-9 and spaces |
| `bad-length` | not 8 or 11 characters |
| `bad-country` | characters 5 and 6 are not both letters |
The checks run in that order; the first failure is the reason. Validators
answer rather than throw.
Not enforced: SWIFT's operational conventions for particular characters in the
location and branch codes, which are directory policy rather than format and
are not in the ISO 20022 pattern.
Sources: ISO 9362:2014 / 2022 "Banking - Banking telecommunication messages -
Business identifier code (BIC)"; the ISO 20022 BICFIDec2014Identifier and
AnyBICDec2014Identifier patterns (https://www.iso20022.org); SWIFT, "Business
Identifier Code (BIC)" (https://www.swift.com/standards/data-standards/bic-business-identifier-code).