Skip to content

Commit

Permalink
Added HU BBAN validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Simplify committed Dec 5, 2021
1 parent 029b643 commit 8319e6f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2021-12-05 Saša Jovanić <sasa@simplify.ba>
* Added Hungarian (HU) BBAN validation

2021-12-01 Saša Jovanić <sasa@simplify.ba>
* Improve test coverage

Expand Down
45 changes: 44 additions & 1 deletion src/IBANTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,43 @@ const checkFrenchBBAN = (bban: string): boolean => {
return remainder === 0;
};

/**
* Hungarian (HU) BBAN check
*
* @ignore
*/
const checkHungarianBBAN = (bban: string): boolean => {
const weights = [9, 7, 3, 1, 9, 7, 3, 1, 9, 7, 3, 1, 9, 7, 3];
const controlDigitBankBranch = parseInt(bban.charAt(7), 10);
const toCheckBankBranch = bban.substring(0, 7);
let sum = 0;
for (let index = 0; index < toCheckBankBranch.length; index++) {
sum += parseInt(toCheckBankBranch.charAt(index), 10) * weights[index];
}
const remainder = sum % 10;
if (controlDigitBankBranch !== (remainder === 0 ? 0 : 10 - remainder)) {
return false;
}
sum = 0;
if (bban.endsWith('00000000')) {
const toCheckAccount = bban.substring(8, 15);
const controlDigitAccount = parseInt(bban.charAt(15), 10);
for (let index = 0; index < toCheckAccount.length; index++) {
sum += parseInt(toCheckAccount.charAt(index), 10) * weights[index];
}
const remainder = sum % 10;
return controlDigitAccount === (remainder === 0 ? 0 : 10 - remainder);
} else {
const toCheckAccount = bban.substring(8, 23);
const controlDigitAccount = parseInt(bban.charAt(23), 10);
for (let index = 0; index < toCheckAccount.length; index++) {
sum += parseInt(toCheckAccount.charAt(index), 10) * weights[index];
}
const remainder = sum % 10;
return controlDigitAccount === (remainder === 0 ? 0 : 10 - remainder);
}
};

/**
* Country specifications
*/
Expand Down Expand Up @@ -1079,7 +1116,13 @@ export const countrySpecs: CountryMapInternal = {
SEPA: true,
},
HT: {},
HU: { chars: 28, bban_regexp: '^[0-9]{24}$', IBANRegistry: true, SEPA: true },
HU: {
chars: 28,
bban_regexp: '^[0-9]{24}$',
bban_validation_func: checkHungarianBBAN,
IBANRegistry: true,
SEPA: true,
},
ID: {},
IE: {
chars: 22,
Expand Down
12 changes: 12 additions & 0 deletions test/ibantools_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,18 @@ describe('IBANTools', function() {
it('with valid MC IBAN should return true', function() {
expect(iban.isValidIBAN('MC1112739000700011111000H79')).to.be.true;
});
it('with valid HU IBAN should return true', function() {
expect(iban.isValidIBAN('HU42117730161111101800000000')).to.be.true;
});
it('with valid HU IBAN should return true', function() {
expect(iban.isValidIBAN('HU51100320000122013950000249')).to.be.true;
});
it('with valid HU IBAN should return true', function() {
expect(iban.isValidIBAN('HU43100320000122032850002447')).to.be.true;
});
it('with valid HU IBAN should return true', function() {
expect(iban.isValidIBAN('HU90100320000160120200000000')).to.be.true;
});
});

describe('When calling validateIBAN()', function() {
Expand Down

0 comments on commit 8319e6f

Please sign in to comment.