52 lines
1.5 KiB
JavaScript
52 lines
1.5 KiB
JavaScript
module.exports = function(iban, countryCode) {
|
|
if (iban == null) return true;
|
|
if (typeof iban != 'string') return false;
|
|
if (countryCode?.toLowerCase() != 'es') return true;
|
|
|
|
iban = iban.toUpperCase();
|
|
iban = trim(iban);
|
|
iban = iban.replace(/\s/g, '');
|
|
|
|
if (iban.length != 24)
|
|
return false;
|
|
|
|
// Se coge las primeras dos letras y se pasan a números
|
|
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.
|
|
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
|
|
let resto = module97(isbanaux);
|
|
|
|
if (resto == 1)
|
|
return true;
|
|
|
|
return false;
|
|
|
|
function module97(iban) {
|
|
let parts = Math.ceil(iban.length / 7);
|
|
let remainer = '';
|
|
|
|
for (let i = 1; i <= parts; i++)
|
|
remainer = String(parseFloat(remainer + iban.substr((i - 1) * 7, 7)) % 97);
|
|
|
|
return remainer;
|
|
}
|
|
|
|
function getIbanNumber(letra) {
|
|
let letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
return letters.search(letra) + 10;
|
|
}
|
|
|
|
function trim(text) {
|
|
return (text || '').replace(/^(\s|\u00A0)+|(\s|\u00A0)+$/g, '');
|
|
}
|
|
};
|