UK Sort Codes, Account Numbers, IBANs and SWIFT Codes for Test Data
Test data · November 2026 · 10 min read
How UK bank identifiers are built, which ones carry check digits, working IBAN validation and generation code, and how to get bank details for tests that belong to nobody.
A UK bank account is identified by a six-digit sort code and an eight-digit account number. Abroad, the same account is quoted as a 22-character IBAN, and the bank as an 8- or 11-character SWIFT code. All four have published structures, and two of them carry check digits you can verify offline. This guide covers each one, with code, and how to get test values that pass validation without belonging to anyone.
Sort codes
A sort code identifies the bank and branch. It is six digits, printed in three pairs:
20-00-00
The first one or two digits broadly indicate the bank, and the rest the branch, but the allocation has been reshuffled by decades of mergers. Treat a sort code as an opaque identifier, not as something you can decode by hand. The authoritative register of which sort codes exist, and which payment schemes each one can receive (Bacs, Faster Payments, CHAPS), is the Extended Industry Sorting Code Directory maintained for the UK payments industry.
For validation, the format is simply six digits. Accept them with or without dashes or spaces, store them without, and display them as NN-NN-NN.
Account numbers
A UK account number is eight digits. A few banks historically issued six, seven, nine or ten digit numbers, and there are documented rules for converting those to the standard eight, but anything your users type today should be eight digits. Keep the leading zeros: 00012345 is not the same field value as 12345, and storing it as an integer loses them.
Modulus checking: the check digit you can verify
The sort code and account number together can be checked for internal consistency before any payment is attempted. This is called modulus checking, and the specification and weight tables are published by Pay.UK (previously by Vocalink).
The idea is the same as any check digit. Each of the 14 digits (six of the sort code, eight of the account number) is multiplied by a weight, the products are summed, and the total must divide exactly by a modulus. The complication is that the weights and the method depend on the sort code range:
A sort code range can require one check or two, and a set of numbered exceptions modify the rules for particular banks. In code, a single MOD11 pass looks like this:
// One modulus pass. The weights come from the published table row for the sort code's range.
function modulusPass(sortCode, accountNumber, weights, modulus) {
const digits = (sortCode + accountNumber).split('').map(Number);
const total = digits.reduce((sum, digit, i) => sum + digit * weights[i], 0);
return total % modulus === 0;
}
Do not hand-write the table. It changes, the exceptions are where the real complexity lives, and a wrong row rejects genuine customers. Use a maintained library that ships the current table, or a provider's bank-details validation endpoint. And remember what a pass means: the numbers are consistent with each other. It does not prove the account exists or is open. Only the payment itself, or a Confirmation of Payee check, tells you that.
IBANs
An IBAN wraps the same sort code and account number in an international envelope. A UK IBAN is always 22 characters:
GB82 WEST 1234 5698 7654 32
│ │ │ │ └──────── account number (8)
│ │ │ └─────────────── sort code (6)
│ │ └──────────────────── bank identifier (4 letters, from the BIC)
│ └─────────────────────── check digits (2)
└───────────────────────── country code
GB82WEST12345698765432 is the standard example used in banking documentation, and it is not a real account.
The IBAN check is the same for every country (ISO 7064, MOD 97-10): move the first four characters to the end, replace each letter with two digits (A = 10, B = 11 … Z = 35), and the resulting number modulo 97 must be 1.
function isValidUkIban(input) {
const iban = input.toUpperCase().replace(/\s+/g, '');
if (!/^GB\d{2}[A-Z]{4}\d{14}$/.test(iban)) return false;
const rearranged = iban.slice(4) + iban.slice(0, 4);
const numeric = rearranged.replace(/[A-Z]/g, (c) => String(c.charCodeAt(0) - 55));
// Process digit by digit: the full number is far too large for a JavaScript Number.
let remainder = 0;
for (const digit of numeric) remainder = (remainder * 10 + Number(digit)) % 97;
return remainder === 1;
}
isValidUkIban('GB82 WEST 1234 5698 7654 32'); // true
isValidUkIban('GB83 WEST 1234 5698 7654 32'); // false
To build a valid IBAN for test data, run the same calculation with the check digits set to 00, then use 98 − remainder as the check digits:
function buildUkIban(bankCode, sortCode, accountNumber) {
const bban = bankCode + sortCode + accountNumber;
const numeric = (bban + 'GB00').replace(/[A-Z]/g, (c) => String(c.charCodeAt(0) - 55));
let remainder = 0;
for (const digit of numeric) remainder = (remainder * 10 + Number(digit)) % 97;
return 'GB' + String(98 - remainder).padStart(2, '0') + bban;
}
buildUkIban('TEST', '000000', '12345678'); // 'GB21TEST00000012345678'
The IBAN check catches typing errors in the whole string. It does not run the UK modulus check on the sort code and account number inside it, so a well-formed IBAN can still carry an account number that fails modulus checking.
SWIFT codes (BIC)
A SWIFT code, formally a Business Identifier Code, identifies the bank rather than the account:
NWBK GB 2L XXX
└──┘ └┘ └┘ └─┘
bank country location branch (optional)
It is 8 or 11 characters: four letters for the institution, two for the country, two letters or digits for the location, and an optional three for the branch. XXX as the branch means the head office, so an 8-character code and the same code with XXX appended refer to the same place. There is no check digit; the only offline validation is the shape:
const BIC = /^[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}([A-Z0-9]{3})?$/;
Whether a BIC is real, and which bank it belongs to, needs the SWIFT directory or a provider that licenses it. For a UK IBAN, the four-letter bank identifier is the first four characters of the bank's BIC.
Getting test values
For automated tests you want values that pass validation but belong to nobody:
- Use your payment provider's sandbox values first. Every provider that supports Bacs Direct Debit or UK bank transfers documents test sort codes and account numbers for its sandbox, usually with specific numbers that trigger success, failure or a pending state. They are the only values guaranteed to behave predictably end to end, so look them up in the provider's testing documentation rather than inventing your own.
- For pure validation tests, generate. Build IBANs with the function above around an obviously fictional bank code such as
TEST. For sort code and account number pairs that must pass modulus checking, generate with the same library you validate with, so the two never disagree. - Never copy real bank details into fixtures, including your own or a colleague's. A sort code and account number is enough to set up a Direct Debit in someone's name, and test databases are copied, shared and backed up far more casually than production. The wider argument is in using real customer data in test environments.
Pair the bank details with a fictional account holder. The name and persona generator and the UK address generator give you a name and a structurally valid address that is never a live delivery point.
Summary
Validate the structure in the form, verify existence with the authority or the payment itself, and keep real values out of your test data. The same three-step approach for VAT numbers is in the UK VAT number format explained.
More guides
- How to Check if a UK Address Is Real (and What “Valid” Actually Means)
- UK Bank Account Numbers: How Many Digits, How to Read One, and How to Check It
- The National Insurance Number Format Explained, with a Validator
- The NHS Number Format and Check Digit Explained
- UK Company Registration Numbers: Format and Prefixes Explained
- What Is a Post Town? How to Find Yours, and Why It Isn’t Always Your Town
- The UK Driving Licence Number Format Explained
- How Address Verification Software Works: PAF, UPRNs, Match Levels and What to Ask a Vendor
- Data Masking, Anonymisation and Pseudonymisation Under UK GDPR: What Each One Actually Means
- Validating UK Phone Numbers: Formats, Ofcom Ranges and the Mistakes Every Regex Makes
- Mocking a UK Address Lookup API in Your Tests
- Testing KYC and Identity Verification Flows Without Real Identities
- Test Data Management: Production Copies, Masked Data and Synthetic Data
- The UK VAT Number Format Explained, with a Check-Digit Validator
- The UK Postcode Format Explained: Areas, Districts, Sectors and Units
- Royal Mail Address Formatting: How to Lay Out a UK Address Correctly
- How UK Address Generators Work — and How to Tell a Good One
- Using Real Customer Data in Test Environments: The UK GDPR Problem
- Why Your Address Form Rejects Perfectly Valid UK Addresses
- The UK Telephone Numbering Plan, and the Numbers Reserved for Fiction
- Validating UK Postcodes in JavaScript, Python and SQL
- Where UK Address Data Comes From: PAF, ONSPD, OS Open Names and AddressBase
- Why Faker's UK Postcodes Fail Validation