A SWIFT code has no check digit, so your validator needs to read every character

2026-11-30来源 dev.toEnglish

Every international wire transfer starts with eight to eleven characters: the SWIFT/BIC code. Unlike a credit card number, which has the Luhn check, or a US routing number, which has the 3-7-1 weighted check, a BIC has no check digit at all. Nothing in the code itself can catch a mistyped letter. The only thing a validator can do is read the structure, field by field, and that is exactly what ISO 9362 defines.

A BIC has four parts. Characters 1 to 4 are the bank code, always letters. Characters 5 and 6 are the country code from ISO 3166-1. Characters 7 and 8 are the location code. Characters 9 to 11, when present, are the branch code.

Take DEUTDEFF500:

DEUT bank code DE country code (Germany) FF location code 500 branch code 

Take CHASUS33XXX:

CHAS bank code US country code (United States) 33 location code XXX branch code (head office) 

The 8-character form drops the branch entirely. DEUTDEFF is the same bank in its primary-office form, and XXX as a branch code means the head office too.

Two characters carry hidden meaning. The second character of the location code is a flag: 0 means a test BIC, used for testing only and never for live payments, and 1 means a passive SWIFT participant, an institution that receives through SWIFT but does not send. Any other second character means a live, active participant. So DEUTDEF0 would be a test code and DEUTDEF1 a passive one, while DEUTDEFF is live.

Here is the whole validator in JavaScript:

const COUNTRIES = { DE: 'Germany', US: 'United States', GB: 'United Kingdom', CH: 'Switzerland', JP: 'Japan', FR: 'France', ES: 'Spain' /* ...all 249 ISO 3166-1 codes */ }; function validateBic(raw) { const code = raw.replace(/[\s-]/g, '').toUpperCase(); if (code.length !== 8 && code.length !== 11) return { valid: false, reason: 'wrong length' }; const bank = code.slice(0, 4); if (/[^A-Z]/.test(bank)) return { valid: false, reason: 'bank code must be letters' }; const country = code.slice(4, 6); if (/[^A-Z]/.test(country)) return { valid: false, reason: 'country code must be letters' }; if (!COUNTRIES[country]) return { valid: false, reason: 'unknown country code' }; return { valid: true, bank, country: COUNTRIES[country], location: code.slice(6, 8), test: code[7] === '0', passive: code[7] === '1', branch: code.length === 11 ? code.slice(8) : null, headOffice: code.slice(8) === 'XXX', }; } 

A few things to note if you build your own. First, normalize before you validate: strip spaces and hyphens, uppercase everything, because BICs are case-insensitive and people paste them with spaces. Second, check the fields in order and name the exact character and position when one fails, because "invalid" tells the user nothing and "character 2 must be a letter" tells them everything. Third, never guess a bank name from the bank code. There is no public mapping from four letters to a bank, and inventing one is how bad data gets shipped. Country names are safe: ISO 3166-1 is a fixed, public list of 249 codes.

And be honest about what a pass means. A correctly formed BIC is not a registered BIC. The only way to confirm a bank is registered with SWIFT is the official SWIFT BIC directory at swift.com. A structure check catches format errors, not typos in letters, because there is no checksum to catch them. Say that on the page, next to the result, not buried in a footnote.

I built a free validator that does all of this in the browser, with a field-by-field breakdown, bulk validation for up to 100 codes, and the test and passive flags decoded: SWIFT/BIC Code Validator. Nothing you type is uploaded. Try DEUTDEFF500, CHASUS33XXX, and DEUTDEFF to see the three forms.

原文:https://www.a8q9.cn/en/article/a-swift-code-has-no-check-digit-so-your-validator-needs-to-read-every-character-a00603.html

相关文章