Skip to content

Commit

Permalink
Add Dutch BBAN validation
Browse files Browse the repository at this point in the history
Fixes #415
  • Loading branch information
Simplify committed Dec 2, 2023
1 parent 49cb9e9 commit 772c95e
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 5 deletions.
6 changes: 5 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
2023-10-14 Saša Jovanić <[email protected]>
2023-12-14 Saša Jovanić <[email protected]>
* Version 4.3.7
* Added Dutch (NL) BBAN validation

2023-11-26 Saša Jovanić <[email protected]>
* Version 4.3.6

2023-11-13 Davide Bianco <[email protected]>
Expand Down
34 changes: 33 additions & 1 deletion dist/ibantools.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ define(["require", "exports"], function (require, exports) {
* @package Documentation
* @author Saša Jovanić
* @module ibantools
* @version 4.3.6
* @version 4.3.7
* @license MPL-2.0
* @preferred
*/
Expand Down Expand Up @@ -777,6 +777,37 @@ define(["require", "exports"], function (require, exports) {
return controlDigitAccount === (remainder_2 === 0 ? 0 : 10 - remainder_2);
}
};
/**
* Dutch (NL) BBAN check
*
* @ignore
*/
var checkDutchBBAN = function (bban) {
if (bban === '') {
return false;
}
var weights = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
var toCheckAccount = bban.substring(4, 14);
if (toCheckAccount.startsWith('000')) {
return true;
}
if (toCheckAccount.startsWith('00')) {
return false;
}
var sum = toCheckAccount
.split('')
.map(function (value, index) {
if (value === '0' && index === 0) {
return 0;
}
var number = parseInt(value, 10);
var weight = weights[index];
return number * weight;
})
.reduce(function (a, b) { return a + b; });
console.log(sum);
return sum % 11 === 0;
};
/**
* Set custom BBAN validation function for country.
*
Expand Down Expand Up @@ -1430,6 +1461,7 @@ define(["require", "exports"], function (require, exports) {
NL: {
chars: 18,
bban_regexp: '^[A-Z]{4}[0-9]{10}$',
bban_validation_func: checkDutchBBAN,
IBANRegistry: true,
SEPA: true,
bank_identifier: '0-3',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ibantools",
"version": "4.3.6",
"version": "4.3.7",
"description": "Validation, extraction and creation of IBAN, BBAN, BIC/SWIFT numbers plus some other helpful stuff like ISO 3136-1 alpha 2 country list",
"keywords": [
"IBAN",
Expand Down
28 changes: 27 additions & 1 deletion src/ibantools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @package Documentation
* @author Saša Jovanić
* @module ibantools
* @version 4.3.6
* @version 4.3.7
* @license MPL-2.0
* @preferred
*/
Expand Down Expand Up @@ -894,6 +894,31 @@ const checkHungarianBBAN = (bban: string): boolean => {
}
};

/**
* Dutch (NL) BBAN check
*
* @ignore
*/
const checkDutchBBAN = (bban: string): boolean => {
if(bban === '') { return false; }

const weights = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
const toCheckAccount = bban.substring(4, 14);
if(toCheckAccount.startsWith('000')) { return true; }
if(toCheckAccount.startsWith('00')) { return false; }
const sum: number = toCheckAccount
.split('')
.map((value: string, index: number) => {
if(value === '0' && index === 0) { return 0; }
const number = parseInt(value, 10);
const weight = weights[index];
return number * weight;
})
.reduce((a: number, b: number) => a + b);
console.log(sum);
return sum % 11 === 0;
}

/**
* Set custom BBAN validation function for country.
*
Expand Down Expand Up @@ -1548,6 +1573,7 @@ export const countrySpecs: CountryMapInternal = {
NL: {
chars: 18,
bban_regexp: '^[A-Z]{4}[0-9]{10}$',
bban_validation_func: checkDutchBBAN,
IBANRegistry: true,
SEPA: true,
bank_identifier: '0-3',
Expand Down
11 changes: 10 additions & 1 deletion test/ibantools_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ describe('IBANTools', function() {
it('with valid RU IBAN should return true', function() {
expect(iban.isValidIBAN('RU0204452560040702810412345678901')).to.be.true;
});
it('with valid old Postbank Dutch IBAN should return true', function() {
expect(iban.isValidIBAN('NL08INGB0000000555')).to.be.true;
});
it('with invalid Dutch IBAN should return false', function() {
expect(iban.isValidIBAN('NL08INGB0012345555')).to.be.false;
});
it('with two dots should return false', function() {
expect(iban.isValidIBAN('..')).to.be.false;
});
Expand Down Expand Up @@ -344,6 +350,7 @@ describe('IBANTools', function() {
errorCodes: [
iban.ValidationErrorsIBAN.WrongBBANLength,
iban.ValidationErrorsIBAN.WrongBBANFormat,
iban.ValidationErrorsIBAN.WrongAccountBankBranchChecksum,
iban.ValidationErrorsIBAN.WrongIBANChecksum,
],
});
Expand Down Expand Up @@ -383,7 +390,8 @@ describe('IBANTools', function() {
it('with invalid IBAN checksum should return false with correct code', function() {
expect(iban.validateIBAN('NL91ABNA0517164300')).to.deep.equal({
valid: false,
errorCodes: [iban.ValidationErrorsIBAN.WrongIBANChecksum],
errorCodes: [iban.ValidationErrorsIBAN.WrongAccountBankBranchChecksum,
iban.ValidationErrorsIBAN.WrongIBANChecksum],
});
});

Expand All @@ -400,6 +408,7 @@ describe('IBANTools', function() {
errorCodes: [
iban.ValidationErrorsIBAN.WrongBBANLength,
iban.ValidationErrorsIBAN.WrongBBANFormat,
iban.ValidationErrorsIBAN.WrongAccountBankBranchChecksum,
iban.ValidationErrorsIBAN.ChecksumNotNumber,
iban.ValidationErrorsIBAN.WrongIBANChecksum,
],
Expand Down

0 comments on commit 772c95e

Please sign in to comment.