Skip to content

Commit

Permalink
Added NL bank account validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Simplify committed Nov 24, 2021
1 parent 11a3cd6 commit 0343e7c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 7 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2021-11-24 Saša Jovanić <sasa@simplify.ba>
* Added Netherlands (NL) extra BBAN validation
* Added extra error code when validating IBAN `WrongAccountBankBranchChecksum` that indicates when checksum for account number or bank or branch code is incorrect

2021-11-23 Saša Jovanić <sasa@simplify.ba>
* Version 4.0.1
* Fixed bug when validating Spain IBAN
Expand Down
3 changes: 2 additions & 1 deletion dist/ibantools.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export declare enum ValidationErrorsIBAN {
WrongBBANLength = 2,
WrongBBANFormat = 3,
ChecksumNotNumber = 4,
WrongIBANChecksum = 5
WrongIBANChecksum = 5,
WrongAccountBankBranchChecksum = 6
}
/**
* Interface for ValidateIBAN result
Expand Down
27 changes: 25 additions & 2 deletions 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.0.1
* @version 4.1.0
* @license MPL-2.0
* @preferred
*/
Expand Down Expand Up @@ -56,6 +56,7 @@ define(["require", "exports"], function (require, exports) {
ValidationErrorsIBAN[ValidationErrorsIBAN["WrongBBANFormat"] = 3] = "WrongBBANFormat";
ValidationErrorsIBAN[ValidationErrorsIBAN["ChecksumNotNumber"] = 4] = "ChecksumNotNumber";
ValidationErrorsIBAN[ValidationErrorsIBAN["WrongIBANChecksum"] = 5] = "WrongIBANChecksum";
ValidationErrorsIBAN[ValidationErrorsIBAN["WrongAccountBankBranchChecksum"] = 6] = "WrongAccountBankBranchChecksum";
})(ValidationErrorsIBAN = exports.ValidationErrorsIBAN || (exports.ValidationErrorsIBAN = {}));
/**
* validateIBAN
Expand All @@ -76,10 +77,14 @@ define(["require", "exports"], function (require, exports) {
result.valid = false;
result.errorCodes.push(ValidationErrorsIBAN.WrongBBANLength);
}
if (spec && spec.bban_regexp && !isValidBBAN(iban.slice(4), iban.slice(0, 2))) {
if (spec && spec.bban_regexp && !checkFormatBBAN(iban.slice(4), spec.bban_regexp)) {
result.valid = false;
result.errorCodes.push(ValidationErrorsIBAN.WrongBBANFormat);
}
if (spec && spec.bban_validation_func && !spec.bban_validation_func(iban.slice(4))) {
result.valid = false;
result.errorCodes.push(ValidationErrorsIBAN.WrongAccountBankBranchChecksum);
}
var reg = new RegExp('^[0-9]{2}$', '');
if (!reg.test(iban.slice(2, 4))) {
result.valid = false;
Expand Down Expand Up @@ -447,6 +452,23 @@ define(["require", "exports"], function (require, exports) {
return controlDigit === (remainder === 0 ? 0 : 11 - remainder);
}
};
/**
* Used for Netherlands BBAN check
*
* @ignore
*/
var checkDutchBBAN = function (bban) {
var bbanWithoutSpacesAndPeriods = bban.replace(/[\s.]+/g, '');
var accountNumber = bbanWithoutSpacesAndPeriods.substring(4, 14);
if (accountNumber.startsWith('000')) {
return true; // Postbank account, no `elfproef` possible
}
var sum = 0;
for (var index = 0; index < 10; index++) {
sum += parseInt(accountNumber.charAt(index), 10) * (10 - index);
}
return sum % 11 === 0;
};
/**
* Used for Poland BBAN check
*
Expand Down Expand Up @@ -927,6 +949,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,
},
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.0.1",
"version": "4.1.0",
"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: 26 additions & 2 deletions 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.0.1
* @version 4.1.0
* @license MPL-2.0
* @preferred
*/
Expand Down Expand Up @@ -56,6 +56,7 @@ export enum ValidationErrorsIBAN {
WrongBBANFormat,
ChecksumNotNumber,
WrongIBANChecksum,
WrongAccountBankBranchChecksum,
}

/**
Expand Down Expand Up @@ -85,10 +86,14 @@ export function validateIBAN(iban?: string): ValidateIBANResult {
result.valid = false;
result.errorCodes.push(ValidationErrorsIBAN.WrongBBANLength);
}
if (spec && spec.bban_regexp && !isValidBBAN(iban.slice(4), iban.slice(0, 2))) {
if (spec && spec.bban_regexp && !checkFormatBBAN(iban.slice(4), spec.bban_regexp)) {
result.valid = false;
result.errorCodes.push(ValidationErrorsIBAN.WrongBBANFormat);
}
if (spec && spec.bban_validation_func && !spec.bban_validation_func(iban.slice(4))) {
result.valid = false;
result.errorCodes.push(ValidationErrorsIBAN.WrongAccountBankBranchChecksum);
}
const reg = new RegExp('^[0-9]{2}$', '');
if (!reg.test(iban.slice(2, 4))) {
result.valid = false;
Expand Down Expand Up @@ -529,6 +534,24 @@ const checkNorwayBBAN = (bban: string): boolean => {
}
};

/**
* Used for Netherlands BBAN check
*
* @ignore
*/
const checkDutchBBAN = (bban: string): boolean => {
const bbanWithoutSpacesAndPeriods = bban.replace(/[\s.]+/g, '');
const accountNumber = bbanWithoutSpacesAndPeriods.substring(4, 14);
if (accountNumber.startsWith('000')) {
return true; // Postbank account, no `elfproef` possible
}
let sum = 0;
for (let index = 0; index < 10; index++) {
sum += parseInt(accountNumber.charAt(index), 10) * (10 - index);
}
return sum % 11 === 0;
};

/**
* Used for Poland BBAN check
*
Expand Down Expand Up @@ -1011,6 +1034,7 @@ export const countrySpecs: CountryMapInternal = {
NL: {
chars: 18,
bban_regexp: '^[A-Z]{4}[0-9]{10}$',
bban_validation_func: checkDutchBBAN,
IBANRegistry: true,
SEPA: true,
},
Expand Down
12 changes: 11 additions & 1 deletion test/ibantools_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ describe('IBANTools', function() {
it('with valid IBAN should return true', function() {
expect(iban.isValidIBAN('NL91ABNA0417164300')).to.be.true;
});
it('with valid IBAN should return true', function() {
expect(iban.isValidIBAN('NL50PSTB0000054322')).to.be.true;
});
it('with invalid IBAN should return false', function() {
expect(iban.isValidIBAN('NL91ABNA0517164300')).to.be.false;
});
Expand Down Expand Up @@ -221,7 +224,10 @@ 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 Down Expand Up @@ -259,6 +265,7 @@ describe('IBANTools', function() {
errorCodes: [
iban.ValidationErrorsIBAN.WrongBBANLength,
iban.ValidationErrorsIBAN.WrongBBANFormat,
iban.ValidationErrorsIBAN.WrongAccountBankBranchChecksum,
iban.ValidationErrorsIBAN.ChecksumNotNumber,
iban.ValidationErrorsIBAN.WrongIBANChecksum,
],
Expand Down Expand Up @@ -439,6 +446,9 @@ describe('IBANTools', function() {
it('with valid BBAN and valid country code should return true', function() {
expect(iban.isValidBBAN('ABNA0417164300', 'NL')).to.be.true;
});
it('with valid BBAN and valid country code should return true', function() {
expect(iban.isValidBBAN('PSTB0000054322', 'NL')).to.be.true;
});
it('with invalid BBAN and valid country code should return false', function() {
expect(iban.isValidBBAN('A7NA0417164300', 'NL')).to.be.false;
});
Expand Down

0 comments on commit 0343e7c

Please sign in to comment.