ibantools-germany
Version:
IBAN Validator and Generator for German Bank Accounts
103 lines (100 loc) • 3.62 kB
JavaScript
;
/**
* ibantools-germany
* Copyright (C) 2022-2024 Markus Baumer <markus@baumer.dev>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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;