Integrating VAT ID validation library in Javascript

Hello,

I´ve found this code Spanish DNI, CIF, NIE validator · GitHub that I want to integrate in my app so I can validate spanish companies VAT ID but I don´t know even how to start for implementing this Javascript library in Bubble.

I know there is a plugin that runs on vatlayer API service but vatlayer is paid service and I would love to integrate the Javascript library.

Can anyone point me into the right direction?

Thanks a lot to anyone that can help me a bit. Maybe @johnny or @NigelG or @Jici ?, hehe.

You can use the toolbox :toolbox: utility plugin and use “Run JavaScript” in a workflow.

You copy and paste the JavaScript you see on that GitHub into the “Run JavaScript”

You will need to use the JS to Bubble to get the results into bubble.

Thanks but how do I say to that JavaScript to check the input so I can validate?

As you can see I don’t know where to start. Like should I copy paste the code and what’s next?

How do I link the input to the JavaScript?

What you see on GitHub is basically a JavaScript function from Line 18 to Line 135.

To use the function you copy paste it into the “Run JavaScript” workflow and at the very bottom underneath the copy/paste you call the function by adding the following

const results = ValidateSpanishID()

You need to put in your text value between the parentheses ValidateSpanishID( <—text here—->)

1 Like

I am way from a computer and replying from a phone otherwise I would’ve gave you the exact script to copy/paste into Run JavaScript workflow

lol i didnt know you could see this image

Let me try this out.

Thaks @shawnmi6

Haha yes it does.

ValidateSpanishID = (function() {
‘use strict’;

var DNI_REGEX = /^(\d{8})([A-Z])$/;
var CIF_REGEX = /^([ABCDEFGHJKLMNPQRSUVW])(\d{7})([0-9A-J])$/;
var NIE_REGEX = /^[XYZ]\d{7,8}[A-Z]$/;

var ValidateSpanishID = function( str ) {

// Ensure upcase and remove whitespace
str = str.toUpperCase().replace(/\s/, '');

var valid = false;
var type = spainIdType( str );

switch (type) {
  case 'dni':
    valid = validDNI( str );
    break;
  case 'nie':
    valid = validNIE( str );
    break;
  case 'cif':
    valid = validCIF( str );
    break;
}

return {
  type: type,
  valid: valid
};

};

var spainIdType = function( str ) {
if ( str.match( DNI_REGEX ) ) {
return ‘dni’;
}
if ( str.match( CIF_REGEX ) ) {
return ‘cif’;
}
if ( str.match( NIE_REGEX ) ) {
return ‘nie’;
}
};

var validDNI = function( dni ) {
var dni_letters = “TRWAGMYFPDXBNJZSQVHLCKE”;
var letter = dni_letters.charAt( parseInt( dni, 10 ) % 23 );

return letter == dni.charAt(8);

};

var validNIE = function( nie ) {

// Change the initial letter for the corresponding number and validate as DNI
var nie_prefix = nie.charAt( 0 );

switch (nie_prefix) {
  case 'X': nie_prefix = 0; break;
  case 'Y': nie_prefix = 1; break;
  case 'Z': nie_prefix = 2; break;
}

return validDNI( nie_prefix + nie.substr(1) );

};

var validCIF = function( cif ) {

var match = cif.match( CIF_REGEX );
var letter  = match[1],
    number  = match[2],
    control = match[3];

var even_sum = 0;
var odd_sum = 0;
var n;

for ( var i = 0; i < number.length; i++) {
  n = parseInt( number[i], 10 );

  // Odd positions (Even index equals to odd position. i=0 equals first position)
  if ( i % 2 === 0 ) {
    // Odd positions are multiplied first.
    n *= 2;

    // If the multiplication is bigger than 10 we need to adjust
    odd_sum += n < 10 ? n : n - 9;

  // Even positions
  // Just sum them
  } else {
    even_sum += n;
  }

}

var control_digit = (10 - (even_sum + odd_sum).toString().substr(-1) );
var control_letter = 'JABCDEFGHI'.substr( control_digit, 1 );

// Control must be a digit
if ( letter.match( /[ABEH]/ ) ) {
  return control == control_digit;

// Control must be a letter
} else if ( letter.match( /[KPQS]/ ) ) {
  return control == control_letter;

// Can be either
} else {
  return control == control_digit || control == control_letter;
}

};

return ValidateSpanishID;
})();

const results = ValidateSpanishID()

console.log(results)

I was just going to put the code since it throws an error. Let me check your code.

Thanks!

The forum isn’t letting me format it on my phone, hope it helps.

It gives me an error: Sorry, we have encountered an unexpected error. Please click to file a bug report, including error code 1615324293387x989044986440610800 and a step-by-step explanation of how to reproduce this issue.


  1. XYZ ↩︎

In the console I can see the below thing but I don´t what does it mean:
1. {origin: “native error”, UNCAUGHT: “caught by window.onerror”, code: “1615324384746x794692587841839000”}

  1. UNCAUGHT: "caught by window.onerror"
  2. code: "1615324384746x794692587841839000"
  3. origin: "native error"
  4. __proto__: Object

ValidateSpanishID = (function() {
‘use strict’;

var DNI_REGEX = /^(\d{8})([A-Z])$/;
var CIF_REGEX = /^([ABCDEFGHJKLMNPQRSUVW])(\d{7})([0-9A-J])$/;
var NIE_REGEX = /^[XYZ]\d{7,8}[A-Z]$/;

var ValidateSpanishID = function( str ) {

// Ensure upcase and remove whitespace
str = str.toUpperCase().replace(/\s/, '');

var valid = false;
var type = spainIdType( str );

switch (type) {
  case 'dni':
    valid = validDNI( str );
    break;
  case 'nie':
    valid = validNIE( str );
    break;
  case 'cif':
    valid = validCIF( str );
    break;
}

return {
  type: type,
  valid: valid
};

};

var spainIdType = function( str ) {
if ( str.match( DNI_REGEX ) ) {
return ‘dni’;
}
if ( str.match( CIF_REGEX ) ) {
return ‘cif’;
}
if ( str.match( NIE_REGEX ) ) {
return ‘nie’;
}
};

var validDNI = function( dni ) {
var dni_letters = “TRWAGMYFPDXBNJZSQVHLCKE”;
var letter = dni_letters.charAt( parseInt( dni, 10 ) % 23 );

return letter == dni.charAt(8);

};

var validNIE = function( nie ) {

// Change the initial letter for the corresponding number and validate as DNI
var nie_prefix = nie.charAt( 0 );

switch (nie_prefix) {
  case 'X': nie_prefix = 0; break;
  case 'Y': nie_prefix = 1; break;
  case 'Z': nie_prefix = 2; break;
}

return validDNI( nie_prefix + nie.substr(1) );

};

var validCIF = function( cif ) {

var match = cif.match( CIF_REGEX );
var letter  = match[1],
    number  = match[2],
    control = match[3];

var even_sum = 0;
var odd_sum = 0;
var n;

for ( var i = 0; i < number.length; i++) {
  n = parseInt( number[i], 10 );

  // Odd positions (Even index equals to odd position. i=0 equals first position)
  if ( i % 2 === 0 ) {
    // Odd positions are multiplied first.
    n *= 2;

    // If the multiplication is bigger than 10 we need to adjust
    odd_sum += n < 10 ? n : n - 9;

  // Even positions
  // Just sum them
  } else {
    even_sum += n;
  }

}

var control_digit = (10 - (even_sum + odd_sum).toString().substr(-1) );
var control_letter = 'JABCDEFGHI'.substr( control_digit, 1 );

// Control must be a digit
if ( letter.match( /[ABEH]/ ) ) {
  return control == control_digit;

// Control must be a letter
} else if ( letter.match( /[KPQS]/ ) ) {
  return control == control_letter;

// Can be either
} else {
  return control == control_digit || control == control_letter;
}

};

return ValidateSpanishID;
})();

const results = ValidateSpanishID(“123456789”)
console.log(results)

1 Like

I tested that on my phone and it’s working just fine.

For me it´s not even working on jsfiddle.


  1. XYZ ↩︎

The code is this one, right?

ValidateSpanishID = (function() {
‘use strict’;

var DNI_REGEX = /^(\d{8})([A-Z])$/;
var CIF_REGEX = /^([ABCDEFGHJKLMNPQRSUVW])(\d{7})([0-9A-J])$/;
var NIE_REGEX = /^[XYZ]\d{7,8}[A-Z]$/;

var ValidateSpanishID = function( str ) {

// Ensure upcase and remove whitespace
str = str.toUpperCase().replace(/\s/, '');

var valid = false;
var type = spainIdType( str );

switch (type) {
  case 'dni':
    valid = validDNI( str );
    break;
  case 'nie':
    valid = validNIE( str );
    break;
  case 'cif':
    valid = validCIF( str );
    break;
}

return {
  type: type,
  valid: valid
};
};

var spainIdType = function( str ) {
if ( str.match( DNI_REGEX ) ) {
return ‘dni’;
}
if ( str.match( CIF_REGEX ) ) {
return ‘cif’;
}
if ( str.match( NIE_REGEX ) ) {
return ‘nie’;
}
};

var validDNI = function( dni ) {
var dni_letters = “TRWAGMYFPDXBNJZSQVHLCKE”;
var letter = dni_letters.charAt( parseInt( dni, 10 ) % 23 );

return letter == dni.charAt(8);
};

var validNIE = function( nie ) {

// Change the initial letter for the corresponding number and validate as DNI
var nie_prefix = nie.charAt( 0 );

switch (nie_prefix) {
  case 'X': nie_prefix = 0; break;
  case 'Y': nie_prefix = 1; break;
  case 'Z': nie_prefix = 2; break;
}

return validDNI( nie_prefix + nie.substr(1) );
};

var validCIF = function( cif ) {

var match = cif.match( CIF_REGEX );
var letter  = match[1],
    number  = match[2],
    control = match[3];

var even_sum = 0;
var odd_sum = 0;
var n;

for ( var i = 0; i < number.length; i++) {
  n = parseInt( number[i], 10 );

  // Odd positions (Even index equals to odd position. i=0 equals first position)
  if ( i % 2 === 0 ) {
    // Odd positions are multiplied first.
    n *= 2;

    // If the multiplication is bigger than 10 we need to adjust
    odd_sum += n < 10 ? n : n - 9;

  // Even positions
  // Just sum them
  } else {
    even_sum += n;
  }

}

var control_digit = (10 - (even_sum + odd_sum).toString().substr(-1) );
var control_letter = 'JABCDEFGHI'.substr( control_digit, 1 );

// Control must be a digit
if ( letter.match( /[ABEH]/ ) ) {
  return control == control_digit;

// Control must be a letter
} else if ( letter.match( /[KPQS]/ ) ) {
  return control == control_letter;

// Can be either
} else {
  return control == control_digit || control == control_letter;
}
};

return ValidateSpanishID;
})();

const results = ValidateSpanishID(“123456789”)
console.log(results)

It worked, the problem was with the accents since because of my configuration.

How can I expose the result in a text or in an alert?

@shawnmi6 do you know how can I expose the result in a text or alert?