diff --git a/services/loopback/common/validations/specs/validateIban.spec.js b/services/loopback/common/validations/specs/validateIban.spec.js new file mode 100644 index 000000000..a5d08d4c4 --- /dev/null +++ b/services/loopback/common/validations/specs/validateIban.spec.js @@ -0,0 +1,21 @@ +const validateIban = require('../validateIban'); + +describe('IBAN validation', () => { + it('should return false for non-IBAN input', () => { + let isValid = validateIban('Pepinillos'); + + expect(isValid).toBeFalsy(); + }); + + it('should return false for invalid spanish IBAN input', () => { + let isValid = validateIban('ES00 9999 0000 9999 0000 9999'); + + expect(isValid).toBeFalsy(); + }); + + it('should return true for valid spanish IBAN', () => { + let isValid = validateIban('ES91 2100 0418 4502 0005 1332'); + + expect(isValid).toBeTruthy(); + }); +}); diff --git a/services/loopback/common/validations/validateIban.js b/services/loopback/common/validations/validateIban.js index 2e8883642..3ca09ef95 100644 --- a/services/loopback/common/validations/validateIban.js +++ b/services/loopback/common/validations/validateIban.js @@ -6,44 +6,41 @@ module.exports = function(iban) { iban = trim(iban); iban = iban.replace(/\s/g, ''); - if (iban.length != 24) { + if (iban.length != 24) return false; - } // Se coge las primeras dos letras y se pasan a números - var letter1 = iban.substring(0, 1); - var letter2 = iban.substring(1, 2); - var num1 = getIbanNumber(letter1); - var num2 = getIbanNumber(letter2); + let letter1 = iban.substring(0, 1); + let letter2 = iban.substring(1, 2); + let num1 = getIbanNumber(letter1); + let num2 = getIbanNumber(letter2); // Se sustituye las letras por números. - var isbanaux = String(num1) + String(num2) + iban.substring(2); + let isbanaux = String(num1) + String(num2) + iban.substring(2); // Se mueve los 6 primeros caracteres al final de la cadena. isbanaux = isbanaux.substring(6) + isbanaux.substring(0, 6); // Se calcula el resto, llamando a la función module97, definida más abajo - var resto = module97(isbanaux); + let resto = module97(isbanaux); - if (resto == 1) { + if (resto == 1) return true; - } return false; function module97(iban) { - var parts = Math.ceil(iban.length / 7); - var remainer = ''; + let parts = Math.ceil(iban.length / 7); + let remainer = ''; - for (var i = 1; i <= parts; i++) { + for (let i = 1; i <= parts; i++) remainer = String(parseFloat(remainer + iban.substr((i - 1) * 7, 7)) % 97); - } return remainer; } function getIbanNumber(letra) { - var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + let letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; return letters.search(letra) + 10; }