UNPKG

ibantools-germany

Version:

IBAN Validator and Generator for German Bank Accounts

91 lines (90 loc) 2.99 kB
"use strict"; /** * ibantools-germany * Copyright (c) 2022-2026 Markus Baumer <markus@baumer.dev> * SPDX-License-Identifier: MIT OR MPL-2.0 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.isValidIBAN = exports.isValidBBAN = exports.isValidAccountNumberBLZ = void 0; const data_1 = require("./data"); const extract_1 = require("./extract"); const helper_1 = require("./helper"); const method_dispatch_1 = require("./method-dispatch"); /** * Validate bank account number and BLZ * * IMPORTANT: A positive result does not does not necessarily mean that * the account exists; it only checks for structure and check digit! * * @param accountNumber Account number with up to 10 digits * @param blz German BLZ with 8 digits * @returns */ const isValidAccountNumberBLZ = (accountNumber, blz) => { if (!accountNumber || !accountNumber.match(/^\d{1,10}$/) || !blz || !blz.match(/^[1-9]\d{7}$/)) { return false; } const method = (0, data_1.methodForBLZ)(blz); if (!method) { return false; } return ["VALID", "NO_CHECK_DIGIT_CALCULATION"].includes((0, method_dispatch_1.methodDispatch)(accountNumber, blz, method)); }; exports.isValidAccountNumberBLZ = isValidAccountNumberBLZ; /** * Validate German BBAN * * IMPORTANT: A positive result does not does not necessarily mean that * the account exists; it only checks for structure and check digit! * * @param bban German BBAN with 18 digits * @returns */ const isValidBBAN = (bban) => { const extracted = (0, extract_1.extractAccountNumberBLZFromBBAN)(bban); if (!extracted) { return false; } return (0, exports.isValidAccountNumberBLZ)(extracted.accountNumber, extracted.blz); }; exports.isValidBBAN = isValidBBAN; /** * Validate (German) IBAN * * If `onlyGermany` is true, it will return false for any IBAN not starting * with "DE". If false, non-German IBAN will be verified but no national * methods will be applied. * * IMPORTANT: A positive result does not does not necessarily mean that * the account exists; it only checks for structure and check digit! * * @param iban German IBAN with 22 chars * @param onlyGermany Allow only German IBANs (default: false) * @returns */ const isValidIBAN = (iban, onlyGermany = false) => { if (typeof iban !== "string") { return false; } const country = iban.slice(0, 2).toUpperCase(); const checkDigit = iban.slice(2, 4); const bban = iban.slice(4); // Validate IBAN check digit const validateAlphanumeric = `${bban}${country}${checkDigit}`; const validateNumberic = (0, helper_1.lettersToDigits)(validateAlphanumeric); if ((0, helper_1.modulo97)(validateNumberic) !== 1) { return false; } if (country !== "DE") { if (onlyGermany) { return false; } return true; } // Validate BBAN return (0, exports.isValidBBAN)(bban); }; exports.isValidIBAN = isValidIBAN;