So far in this series we've covered the NIF, the tax number, and the NISS, the social security number. The last piece of almost every Portuguese onboarding, payroll or billing form is the bank account.

That's what @edyspider/validate-bank-account-pt is for. It's the newest of the three packages, and it does a bit more than validate: it also tells you which bank an account belongs to and its SWIFT/BIC.

TL;DR

npm i @edyspider/validate-bank-account-pt 
import { validateNIB, validateIBAN, getBIC, getBankName, randomIBAN, } from '@edyspider/validate-bank-account-pt'; validateIBAN('PT50000700001234567890160'); // true validateIBAN('PT50000700001234567890161'); // false validateNIB('000700001234567890160'); // true getBIC('PT50000700001234567890160'); // 'BESCPTPL' getBankName('000700001234567890160'); // 'Novo Banco, S.A.' randomIBAN('0035'); // a valid Caixa Geral de Depósitos IBAN 
  • Zero runtime dependencies
  • Works with ES modules, CommonJS and TypeScript (types included)
  • Runs in Node.js 22.12+, Bun, Deno and the browser

NIB vs IBAN

In Portugal, the NIB (Número de Identificação Bancária) is the 21-digit domestic account number. The IBAN is the international format, and for Portugal it's simply PT50 in front of the NIB:

PT50 0007 0000 1234 5678 901 60 │ │ │ │ └─ NIB check digits (2) │ │ │ └─ account number (11) │ │ └─ branch (4) │ └─ bank code (4) └─ country + IBAN check digits 

The bank code is the useful part: the first 4 digits of the NIB identify the bank, which is how getBIC() and getBankName() work.

How the NIB check digits work

The last two digits are a mod 97 check over the first 19:

check digits = 98 - ((first 19 digits × 100) mod 97) 

For 0007000012345678901:

000700001234567890100 mod 97 = 38 98 - 38 = 60 ✅ matches the last two digits 

There's one catch in JavaScript: a 21-digit number is bigger than Number.MAX_SAFE_INTEGER, so Number('000700001234567890100') % 97 silently gives you the wrong answer. You could reach for BigInt, but the classic trick is to compute the remainder one digit at a time:

function mod97(digits) { let remainder = 0; for (const digit of digits) { remainder = (remainder * 10 + Number(digit)) % 97; } return remainder; } function computeCheckDigits(base) { return String(98 - mod97(base + '00')).padStart(2, '0'); } 

The numbers never get bigger than a few thousand, so it works everywhere, with no BigInt needed.

Why every Portuguese IBAN starts with PT50

The IBAN standard (ISO 13616) adds its own mod 97 check digits on top of the national number. But the NIB check digits are designed so the whole NIB already gives a remainder of 1 when divided by 97. When you run the IBAN algorithm on top of that, the result always comes out the same: 50.

So PT12… or PT49… can never be a valid Portuguese IBAN, and validateIBAN() just checks for the PT50 prefix and then validates the NIB behind it. IBANs from other countries return false: this package is deliberately Portugal-only.

Finding the bank and its BIC

If you've ever built a SEPA payment or a payroll export, you know the BIC/SWIFT code is one more field users get wrong. You don't need to ask for it, because the bank code already tells you:

getBIC('PT50003598412336042299633'); // 'CGDIPTPL' getBankName('PT50000700001234567890160'); // 'Novo Banco, S.A.' 

Both return null when the account is invalid or the bank isn't in the list, so they're safe to call on user input.

The package ships a list of 39 Portuguese banks with their BIC, taken from Banco de Portugal's SICOI list. Validation doesn't depend on it, so an account from a bank that isn't listed is still valid, you just get null for the name and BIC. Banks merge and rename, so if you spot a missing or outdated entry, an issue is very welcome.

Using it in a form

Validation is strict: only digits, PT in capitals, and no spaces. That's on purpose, because people paste IBANs in all sorts of formats. Clean the input first:

const input = 'pt50 0007 0000 1234 5678 9016 0'; validateIBAN(input.replace(/\s/g, '').toUpperCase()); // true 

With Zod, you can validate the IBAN and fill in the BIC in one step:

import { z } from 'zod'; import { validateIBAN, getBIC } from '@edyspider/validate-bank-account-pt'; const bankAccountSchema = z .string() .transform((value) => value.replace(/\s/g, '').toUpperCase()) .refine(validateIBAN, { message: 'Invalid Portuguese IBAN' }) .transform((iban) => ({ iban, bic: getBIC(iban) })); bankAccountSchema.parse('PT50 0007 0000 1234 5678 9016 0'); // { iban: 'PT50000700001234567890160', bic: 'BESCPTPL' } 

And with the other two packages from this series, a full employee form check looks like this:

import { validateNIF } from '@edyspider/validate-nif'; import { validateNISS } from '@edyspider/validate-niss'; import { validateIBAN } from '@edyspider/validate-bank-account-pt'; const isValidEmployee = (e: { nif: string; niss: string; iban: string }) => validateNIF(e.nif) && validateNISS(e.niss) && validateIBAN(e.iban); 

Test data with randomNIB() and randomIBAN()

Both generators return valid numbers, optionally for a specific bank:

import { randomNIB, randomIBAN } from '@edyspider/validate-bank-account-pt'; randomNIB(); // random bank from the list randomIBAN('0007'); // a valid Novo Banco IBAN 

The bank code must be a 4-digit string. randomIBAN(7) throws a TypeError instead of silently generating something odd, because 7 and '0007' are not the same thing, and leading zeros matter here.

These are great for seeding SEPA or payroll test data. Just remember they're structurally valid, not real accounts: never use them to send actual money.

Try it online

To check an IBAN without writing code: edyspider.com/tools/iban.

Wrapping up the series

Three packages, one goal: stop copy-pasting the same Portuguese validation snippets from project to project.

Package Validates Generates
@edyspider/validate-nif NIF (tax number) randomNIF()
@edyspider/validate-niss NISS (social security) randomNISS()
@edyspider/validate-bank-account-pt NIB, IBAN + bank and BIC lookup randomNIB(), randomIBAN()

All three share the same approach: no dependencies, ESM and CommonJS, TypeScript types, and strict validation that accepts unknown and never throws.

Is there another Portuguese number you keep validating by hand? Tell me in the comments: it might become the next package.

Found a bug or a bank that's missing? Open an issue. A ⭐ on GitHub is always appreciated.