Skip to content

Commit

Permalink
Added Czech (CZ) BBAN validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Simplify committed Nov 28, 2021
1 parent f9a1083 commit a48f23b
Show file tree
Hide file tree
Showing 3 changed files with 36 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-11-28 Saša Jovanić <sasa@simplify.ba>
* Added Czech (CZ) BBAN validation

2021-11-27 Saša Jovanić <sasa@simplify.ba>
* Added Croatian (HR) BBAN validation

Expand Down
31 changes: 30 additions & 1 deletion src/IBANTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,29 @@ const checkCroatianBBAN = (bban: string): boolean => {
return checkMod1110(bankBranch, controlBankBranch) && checkMod1110(account, controlAccount);
};

const checkCzechBBAN = (bban: string): boolean => {
const weightsPrefix = [10, 5, 8, 4, 2, 1];
const weightsSuffix = [6, 3, 7, 9, 10, 5, 8, 4, 2, 1];
const controlPrefix = parseInt(bban.charAt(9), 10);
const controlSuffix = parseInt(bban.charAt(19), 10);
const prefix = bban.substring(4, 9);
const suffix = bban.substring(10, 19);
let sum = 0;
for (let index = 0; index < prefix.length; index++) {
sum += parseInt(prefix.charAt(index), 10) * weightsPrefix[index];
}
let remainder = sum % 11;
if (controlPrefix !== (remainder === 0 ? 0 : remainder === 1 ? 1 : 11 - remainder)) {
return false;
}
sum = 0;
for (let index = 0; index < suffix.length; index++) {
sum += parseInt(suffix.charAt(index), 10) * weightsSuffix[index];
}
remainder = sum % 11;
return controlSuffix === (remainder === 0 ? 0 : remainder === 1 ? 1 : 11 - remainder);
};

/**
* Country specifications
*/
Expand Down Expand Up @@ -809,7 +832,13 @@ export const countrySpecs: CountryMapInternal = {
IBANRegistry: true,
SEPA: true,
},
CZ: { chars: 24, bban_regexp: '^[0-9]{20}$', IBANRegistry: true, SEPA: true },
CZ: {
chars: 24,
bban_regexp: '^[0-9]{20}$',
bban_validation_func: checkCzechBBAN,
IBANRegistry: true,
SEPA: true,
},
DE: { chars: 22, bban_regexp: '^[0-9]{18}$', IBANRegistry: true, SEPA: true },
DJ: {
chars: 27,
Expand Down
3 changes: 3 additions & 0 deletions test/ibantools_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ describe('IBANTools', function() {
it('with valid SI IBAN should return true', function() {
expect(iban.isValidIBAN('SI56051008000032875')).to.be.true;
});
it('with valid CZ IBAN should return true', function() {
expect(iban.isValidIBAN('CZ6508000000192000145399')).to.be.true;
});
});

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

0 comments on commit a48f23b

Please sign in to comment.